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?
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];
newLogFileName and isLogFile methods are available to achieve the task