将日志写入沙盒mylog.log文件中
//将日志写入沙盒mylog.log文件中 -(void)writeLogMessageToLocationFile:(NSString *)logMessagesString isCover:(BOOL)isCover{ // NSDocumentDirectory 要查找的文件 // NSUserDomainMask 代表从用户文件夹下找 // 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; NSString *loggingPath = [documentsPath stringByAppendingPathComponent:@"/mylog.log"]; NSLog(@"%@",loggingPath); //覆盖文件的原先内容 if(isCover == YES) { [logMessagesString writeToFile:loggingPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; } else { NSFileManager *fileManger = [NSFileManager defaultManager]; if (![fileManger fileExistsAtPath:loggingPath]) { [logMessagesString writeToFile:loggingPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; } NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:loggingPath]; [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾 NSData* stringData = [logMessagesString dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle writeData:stringData]; //追加写入数据 [fileHandle closeFile]; } }
爱code的妖妖