如何停止主项目中框架的DDFileLogger记录日志?

时间:2019-03-29 07:58:54

标签: objective-c cocoalumberjack

主要项目和框架都使用CocoaLumberjack DDFileLogger,我希望框架fileLogger仅记录框架中的日志。并且当前结果是两个记录器都记录了框架和主项目中的所有日志。

// Framework
    NSURL *cacheDir = [[NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] firstObject];
    NSURL *logPath = [cacheDir URLByAppendingPathComponent:@"XXXXFrameworkLog"];
    if (![NSFileManager.defaultManager fileExistsAtPath:logPath.path]) {
        [NSFileManager.defaultManager createDirectoryAtPath:logPath.path withIntermediateDirectories:YES attributes:nil error: nil];
    }
    DDLogFileManagerDefault *manager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:logPath.path];
    DDFileLogger *fileLogger = [[DDFileLogger alloc] initWithLogFileManager:manager];
    fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
    fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
    [DDLog addLogger:fileLogger];

    DDLogDebug(@"DebugInfo0 From XXXXFramework");
    DDLogDebug(@"DebugInfo1 From XXXXFramework");
    DDLogDebug(@"DebugInfo2 From XXXXFramework");
// Main project
    NSURL *cacheDir = [[NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] firstObject];
    NSURL *logPath = [cacheDir URLByAppendingPathComponent:@"MainLog"];
    if (![NSFileManager.defaultManager fileExistsAtPath:logPath.path]) {
        [NSFileManager.defaultManager createDirectoryAtPath:logPath.path withIntermediateDirectories:YES attributes:nil error: nil];
    }
    DDLogFileManagerDefault *manager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:logPath.path];
    DDFileLogger *fileLogger = [[DDFileLogger alloc] initWithLogFileManager:manager];
    fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
    fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
    [DDLog addLogger:fileLogger];

    DDLogDebug(@"DebugInfo0 From Main Project");
    DDLogDebug(@"DebugInfo1 From Main Project");
    DDLogDebug(@"DebugInfo2 From Main Project");

结果,两个日志文件都有相同的日志。

2019/03/29 15:45:57:943  DebugInfo0 From XXXXFramework
2019/03/29 15:45:57:943  DebugInfo1 From XXXXFramework
2019/03/29 15:45:57:943  DebugInfo2 From XXXXFramework
2019/03/29 15:45:57:958  DebugInfo0 From Main Project
2019/03/29 15:45:57:958  DebugInfo1 From Main Project
2019/03/29 15:45:57:958  DebugInfo2 From Main Project

0 个答案:

没有答案