4

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?

Cintu
  • 913
  • 2
  • 16
  • 32

2 Answers2

6

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];
Luiz Durães
  • 744
  • 9
  • 17
  • 1
    `isLogFile` must be implemented telling how to judge a file is a log file. – Smeegol Jul 01 '16 at 06:41
  • Totally agree with the commenter above. Default implementation can be used as reference https://github.com/CocoaLumberjack/CocoaLumberjack/blob/8c3cb19af17e3ad9c63067288b34039b886748ba/Classes/DDFileLogger.m#L292 – tonymontana Jun 05 '19 at 18:02
0

newLogFileName and isLogFile methods are available to achieve the task

Tejas
  • 1,050
  • 12
  • 23