CocoaLumberjack FileLogger logging to multiple files
Asked Answered
R

2

8

I am using this CocoaLumberjack framework to log all my messages in Objective-C design. Now I want to log all errors to one file and all other messages to another file. I know I could use formatter to filter this information. I created two DDFileLogger instances in AppDelegate but these two loggers keep writing into the same file. I wonder if there is a way that I could specify the logging destination so that two loggers write to two different files.

Rocketry answered 16/10, 2012 at 19:5 Comment(2)
I'm having the same issue. Tried the approach in this post, but it isn't working on either the device or the simulator. Did you find a working approach?Pent
Can we have a documentation or a link where I get step by step instruction on integrating Cocoalumberjack with Hockey in SWIFT 2.2 project. All links and hockey apps web site shows all implementation in objective-c. Thanks!Streak
D
7

The key to getting this working is to set up each DDFileLogger with its own DDLogFileManager, with separate log directory paths for each. DDLogFileManager uses the log directory path to determine which file to log to, so if you have two of them pointing to the same directory, they will log to the same log file. So the key is to use separate directories for each log.

For two different log types: "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 to do your 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.

Derwin answered 8/1, 2013 at 10:43 Comment(2)
You can also simplify this by using the default context 0 and adding the loggerOne code. You then only need to add a DDContextWhitelistFilterLogFormatter for context 0 to your default file logger.Hickie
Implicit declaration of SYNC_LOG_OBJC_MACRO isn't allowed in c99. Can anyone help regarding this ??Exoenzyme
H
0

You can achieve something very close by using a new feature (different log level for every logger). See https://github.com/robbiehanson/CocoaLumberjack/wiki/PerLoggerLogLevels. Creating 2 file loggers (one having error level and the other having verbose) would is not exactly as you described, since the error logs will go into both files. Is this good enough?

Horaciohorae answered 21/11, 2013 at 10:26 Comment(1)
I also need this but the key thing is to have the logging go into separate files. So having it go into both files defeats my purpose at least.Hickie

© 2022 - 2024 — McMap. All rights reserved.