从 Cocoa Lumberjack 日志中提取最新的 100 个条目

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"];