从Cocoa Lumberjack日志

时间:2017-11-22 12:41:39

标签: ios objective-c nsdata nsmutabledata cocoalumberjack

我的应用程序使用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"];

1 个答案:

答案 0 :(得分:0)

以下是解决方案,必须将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"];