2012-10-16 3 views
7

이 CocoaLumberjack 프레임 워크를 사용하여 모든 메시지를 Objective-C 디자인에 기록합니다. 이제 모든 오류를 하나의 파일에 기록하고 다른 모든 메시지는 다른 파일에 기록하려고합니다. 이 정보를 필터하기 위해 포맷터를 사용할 수 있다는 것을 알고 있습니다. AppDelegate에서 두 개의 DDFileLogger 인스턴스를 만들었지 만이 두 로거는 동일한 파일에 계속 쓰고 있습니다. 두 로거가 두 개의 다른 파일에 쓰도록 로깅 대상을 지정할 수있는 방법이 있는지 궁금합니다.CocoaLumberjack FileLogger 여러 파일에 로깅

+0

저도 같은 문제에 봉착는. [이 포스트] (http://stackoverflow.com/a/7762344/264775)에서 접근법을 시도했지만 디바이스 나 시뮬레이터에서 작동하지 않습니다. 작동 방식을 찾았습니까? – thegrinner

+0

SWIFT 2.2 프로젝트에서 코코아 짐 잭과 하키를 통합하는 단계별 지침을 얻을 수있는 문서 또는 링크를 얻을 수 있습니까? 모든 링크와 하키 애플 리케이션 웹 사이트는 모든 구현을 objective-c로 보여줍니다. 감사! – Tejas

답변

7

이 작업을 얻으려면 각 DDFileLogger를 각각의 DDFileManager를 설정하고 각각에 대해 별도의 로그 디렉토리 경로를 지정해야합니다. DDLogFileManager는 로그 디렉토리 경로를 사용하여 어떤 파일을 로깅할지 결정하므로 동일한 디렉토리를 가리키는 두 개의 파일이있는 경우 동일한 로그 파일에 기록됩니다. 따라서 핵심은 각 로그에 대해 별도의 디렉토리를 사용하는 것입니다. "한"과 "2": 두 개의 서로 다른 로그 유형의

는 는 여전히 로깅을 수행하는 매크로를 정의 할 필요가 물론

// 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]; 
:

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

이 일 것입니다 나를 위해.

+0

기본 컨텍스트 0을 사용하고 loggerOne 코드를 추가하여이 작업을 단순화 할 수도 있습니다. 그런 다음 컨텍스트 0에 대한 DDContextWhitelistFilterLogFormatter를 기본 파일 로거에 추가하기 만하면됩니다. – dbainbridge

+0

SYNC_LOG_OBJC_MACRO의 암시 적 선언은 c99에서 허용되지 않습니다. 아무도 이것에 관해서 도울 수 있습니까 ?? –

0

새로운 기능 (모든 로거에 대해 서로 다른 로그 수준)을 사용하여 매우 근접하게 달성 할 수 있습니다. https://github.com/robbiehanson/CocoaLumberjack/wiki/PerLoggerLogLevels을 참조하십시오. 두 파일 로거 (오류 수준 및 자세한 정보 표시가있는 파일 로거)를 만드는 것은 오류 로그가 두 파일에 모두 포함되므로 설명 된대로 정확하지 않습니다. 이것으로 충분합니까?

+0

필자도이 작업이 필요하지만 중요한 것은 로깅을 개별 파일로 이동하는 것입니다. 두 파일에 모두 들어가면 적어도 내 목적을 저지합니다. – dbainbridge

2

나는 위의 상단 답변에 의견을 충분히 명성을 가지고 있지 않지만, 여기에서 최신 CocoaLumberjack와 함께 작동 KabukiAdam의 대답의 버전입니다 :

// 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]; 
+0

SYNC_LOG_OBJC_MACRO의 암시 적 선언은 c99에서 허용되지 않습니다. 아무도 이것에 관해서 도울 수 있습니까 ?? –

관련 문제