CocoaLumberjack iOS - Can we change the logfile name and directory?

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I am using CocoaLumberjack in my project. I need to change the name of the logfile to my custom file name.

NSString * applicationDocumentsDirectory = [[[[NSFileManager defaultManager]                                               URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path]; DDLogFileManagerDefault *documentsFileManager = [[DDLogFileManagerDefault alloc]                                                  initWithLogsDirectory:applicationDocumentsDirectory];  DDFileLogger *fileLogger = [[DDFileLogger alloc]                             initWithLogFileManager:documentsFileManager];     // Configure File Logger [fileLogger setMaximumFileSize:(1024 * 1024)]; [fileLogger setRollingFrequency:(3600.0 * 24.0)]; [[fileLogger logFileManager] setMaximumNumberOfLogFiles:1]; [DDLog addLogger:fileLogger];

By the above code i have changed the directory to the Documents. But now i need to change the logfile name also. How can i achieve this? Is it possible?

回答1:

Although I believe that my reply might be too late, please find below my solution:

1) Inherit DDLogFileManagerDefault and override methods: newLogFileName and isLogFile

#import "DDFileLogger.h"  @interface BaseLogFileManager : DDLogFileManagerDefault  @end  #import "BaseLogFileManager.h"  @implementation BaseLogFileManager  -(NSString *)newLogFileName {     NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];     NSString *timeStamp = [self getTimestamp];      return [NSString stringWithFormat:@"%@%@.log", appName, timeStamp]; }  -(BOOL)isLogFile:(NSString *)fileName {     return NO; }  -(NSString *)getTimestamp {     static dispatch_once_t onceToken;     static NSDateFormatter *dateFormatter;     dispatch_once(&onceToken, ^{         dateFormatter = [NSDateFormatter new];         [dateFormatter setDateFormat:@"YYYY.MM.dd-HH.mm.ss"];     });      return [dateFormatter stringFromDate:NSDate.date]; }  @end

2) In AppDelegate, change following line:

DDLogFileManagerDefault *documentsFileManager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:applicationDocumentsDirectory];

TO:

DDLogFileManagerDefault *documentsFileManager = [[BaseLogFileManager alloc] initWithLogsDirectory:applicationDocumentsDirectory];


回答2:

newLogFileName and isLogFile methods are available to achieve the task



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!