最简单的方法将iOS中的NSLog写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifdef DEBUG
- (void) redirectConsoleLogToDocumentFolder {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"iOS.log"];
freopen([logPath cStringUsingEncoding:NSUTF8StringEncoding],"a+",stderr);
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *time = [formatter stringFromDate:[NSDate date]];
NSLog(@"-------------- Start Log [%@] --------------",time);
[formatter release];
}
#endif

在程序启动的时候调用即可,所有的NSLog日志都会记录到documents下的iOS.log内

1
2
3
#ifdef DEBUG
[self redirectConsoleLogToDocumentFolder];
#endif