CocoaLumberjack FileLogger registers in multiple files

I use this CocoaLumberjack framework to log all of my posts in an Objective-C design. Now I want to write all errors to one file and all other messages to another file. I know that I can use a formatter to filter this information. I created two instances of DDFileLogger in AppDelegate, but these two loggers continue to write to the same file. I wonder if there is a way to specify the registration destination so that the two registrars write two different files.

+7
source share
3 answers

The key to this work is to configure each DDFileLogger with its own DDLogFileManager with separate log directory paths for each. DDLogFileManager uses the path to the log directory to determine which file to log into, so if you have two of them pointing to the same directory, they will be logged in the same log file. Thus, the key is to use separate directories for each log.

For two different types of magazines: One and Two:

// Set the base log directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *baseDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"]; // set up file logger One to log to subdirectory "One" DDLogFileManagerDefault *fileManagerOne = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:[logsDirectory stringByAppendingPathComponent:@"One"]]; DDFileLogger *loggerOne = [[DDFileLogger alloc] fileManagerOne]; // Use the filter formatter to make sure only "One" logs go to the "One" log files ContextWhitelistFilterLogFormatter *formatterOne = [[ContextWhitelistFilterLogFormatter alloc] init]; [formatterOne addToWhitelist:LOG_CONTEXT_ONE]; [loggerOne formatterOne]; [DDLog loggerOne]; // set up file logger Two to log to subdirectory "Two" DDLogFileManagerDefault *fileManagerTwo = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:[logsDirectory stringByAppendingPathComponent:@"Two"]]; DDFileLogger *loggerTwo = [[DDFileLogger alloc] fileManagerTwo]; // Use the filter formatter to make sure only "Two" logs go to the "Two" log files ContextWhitelistFilterLogFormatter *formatterTwo = [[ContextWhitelistFilterLogFormatter alloc] init]; [formatterTwo addToWhitelist:LOG_CONTEXT_TWO]; [loggerTwo formatterTwo]; [DDLog loggerTwo]; 

then of course you still need to define macros for logging:

 #define LOG_CONTEXT_ONE 1 #define LOG_CONTEXT_TWO 2 #define LogOne(frmt, ...) SYNC_LOG_OBJC_MACRO(0, 0, LOG_CONTEXT_ONE, frmt, ##__VA_ARGS__) #define LogTwo(frmt, ...) SYNC_LOG_OBJC_MACRO(0, 0, LOG_CONTEXT_TWO, frmt, ##__VA_ARGS__) 

This is what worked for me.

+7
source

I don't have enough reputation to comment on the top answer above, but here is the version of KabukiAdam's answer that works with the latest CocoaLumberjack:

 // Set the base log directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *baseDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"Logs"]; // set up file logger One to log to subdirectory "One" DDLogFileManagerDefault *fileManagerOne = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:[logsDirectory stringByAppendingPathComponent:@"One"]]; DDFileLogger *loggerOne = [[DDFileLogger alloc] initWithLogFileManager:fileManagerOne]; // Use the filter formatter to make sure only "One" logs go to the "One" log files ContextWhitelistFilterLogFormatter *formatterOne = [[ContextWhitelistFilterLogFormatter alloc] init]; [formatterOne addToWhitelist:LOG_CONTEXT_ONE]; [loggerOne setLogFormatter:formatterOne]; [DDLog addLogger:loggerOne]; // set up file logger One to log to subdirectory "Two" DDLogFileManagerDefault *fileManagerTwo = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:[logsDirectory stringByAppendingPathComponent:@"Two"]]; DDFileLogger *loggerTwo = [[DDFileLogger alloc] initWithLogFileManager:fileManagerTwo]; // Use the filter formatter to make sure only "Two" logs go to the "Two" log files ContextWhitelistFilterLogFormatter *formatterTwo = [[ContextWhitelistFilterLogFormatter alloc] init]; [formatterTwo addToWhitelist:LOG_CONTEXT_TWO]; [loggerTwo setLogFormatter:formatterTwo]; [DDLog addLogger:loggerTwo]; 
+2
source

You can achieve something very close using the new function (a different log level for each registrar). See https://github.com/robbiehanson/CocoaLumberjack/wiki/PerLoggerLogLevels . Creating 2 file registrars (one of which has an error level, and the other a detailed one) will not be the same as you described, since the error logs will fall into both files. Is this good enough?

0
source

All Articles