![](/img/trans.png)
[英]Immediately flushing log statements using the Cocoa Lumberjack logging framework, the way NSLog flushes to console
[英]Extract the latest 100 entries from Cocoa Lumberjack log
我的應用程序使用Cocoa Lumberjack作為日志記錄框架,它創建了幾個需要聚合的日志文件。
在某些時候,我需要將調試數據作為電子郵件附件發送。 整個日志太長,如何獲取最新的100條日志條目?
我目前正在使用NSData
對象將數據保存為字節緩沖區,並且默認情況下不提供逐行讀取。
初始化日志記錄和變量(在應用程序的其他位置完成):
[DDLog addLogger:[DDTTYLogger sharedInstance]];
NSArray *pathsDocs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsDocs objectAtIndex:0];
DDLogFileManagerDefault *documentsFileManager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:documentsDirectory];
self.fileLogger = [[DDFileLogger alloc] initWithLogFileManager:documentsFileManager];
[DDLog addLogger:self.fileLogger];
發送日志的方法:
NSArray* logFilePaths = [[self.fileLogger logFileManager] sortedLogFilePaths];
NSMutableArray* logFileDataArray = [[NSMutableArray alloc] init];
// Collect log file paths
for (NSString* logFilePath in logFilePaths) {
NSURL* fileURL = [NSURL fileURLWithPath:logFilePath];
NSData* logFileData = [NSData dataWithContentsOfURL:fileURL];
if (logFileData) {
// Insert at front to reverse the order, so that oldest logs appear first.
[logFileDataArray insertObject:logFileData atIndex: 0];
}
}
NSMutableData* attachmentData = [[NSMutableData alloc] init];
// Collect log data from all log files
for (NSData* logFileData in logFileDataArray) {
[attachmentData appendData: logFileData];
}
// Convert `NSData` to `NSString`
NSString *logDataString = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
// Extract the 100 most recent entries (rows) from `attachmentData`
// Convert `NSString` back to `NSData`
NSData* logDataFinal = [logDataString dataUsingEncoding:NSUTF8StringEncoding];
// Add log data as mail attachment
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
[mail addAttachmentData:logDataFinal mimeType: @"text/plain" fileName: @"diagnostic.log"];
//[mail addAttachmentData:attachmentData mimeType: @"text/plain" fileName: @"diagnostic.log"];
這是解決方案,必須將其從NSData
轉換為NSArray
才能起作用。
NSString *logDataString = [[NSString alloc] initWithData:attachmentData encoding:NSUTF8StringEncoding];
NSMutableArray * logDataArray = [[NSMutableArray alloc] initWithArray:[logDataString componentsSeparatedByString:@"\n"] copyItems: YES];
unsigned int numberOfLinesToSend = 300;
unsigned int minRange = 0;
unsigned int maxRange = 0;
// Calculate the range
if (numberOfLinesToSend != 0) {
// If the number of lines is greater than 0, otherwise keep both min and max set to 0
if ([logDataArray count]-1>numberOfLinesToSend) {
// If the range does not exceed the number of entries, set both min and max
minRange = [logDataArray count]-1-numberOfLinesToSend;
maxRange = [logDataArray count]-minRange;
} else {
// Otherwise set to full range
axRange = [logDataArray count];
}
}
DDLogInfo(@"Creating log: [logDataArray count]=%i",[logDataArray count]);
DDLogInfo(@"Creating log: NSMakeRange(%i,%i)",minRange, maxRange);
// Extract the 100 most recent entries (rows) from `attachmentData`
NSArray * selectedLinesArray = [logDataArray subarrayWithRange:NSMakeRange(minRange,maxRange)];
// Convert `NSArray` back to `NSString`
NSString * selectedLinesString = [selectedLinesArray componentsJoinedByString:@"\n"];
// Convert `NSString` back to `NSData`
NSData* logDataFinal = [selectedLinesString dataUsingEncoding:NSUTF8StringEncoding];
// Add log data as mail attachment
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
[mail addAttachmentData:logDataFinal mimeType: @"text/plain" fileName: @"diagnostic.log"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.