CocoaLumberjack FileLogger记录到多个文件 [英] CocoaLumberjack FileLogger logging to multiple files

查看:526
本文介绍了CocoaLumberjack FileLogger记录到多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此CocoaLumberjack框架将所有消息记录在Objective-C设计中.现在,我想将所有错误记录到一个文件中,并将所有其他消息记录到另一个文件中.我知道我可以使用格式化程序来过滤此信息.我在AppDelegate中创建了两个DDFileLogger实例,但是这两个记录器继续写入同一文件.我想知道是否有一种方法可以指定日志记录目标,以便两个记录器写入两个不同的文件.

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.

推荐答案

要实现此目的,关键是用自己的DDLogFileManager设置每个DDFileLogger,并为每个DDLogFileManager设置单独的日志目录路径. DDLogFileManager使用日志目录路径来确定要记录到的文件,因此,如果您有两个指向同一目录,则它们将记录到同一日志文件.因此,关键是为每个日志使用单独的目录.

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__)

这对我有用.

这篇关于CocoaLumberjack FileLogger记录到多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆