Last active
June 21, 2019 07:17
-
-
Save 1ookup/fd315883d8fe1260d7e176c9e06b907a to your computer and use it in GitHub Desktop.
初始化DDLog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DDLogInit.h | |
// ProtocolRecord | |
// | |
#import <Foundation/Foundation.h> | |
#import <CocoaLumberjack/CocoaLumberjack.h> | |
static const DDLogLevel ddLogLevel = DDLogLevelVerbose; | |
NS_ASSUME_NONNULL_BEGIN | |
@interface MyCustomFormatter : NSObject <DDLogFormatter> { | |
int loggerCount; | |
NSDateFormatter *threadUnsafeDateFormatter; | |
} | |
@end | |
@interface MyLogFileManager : DDLogFileManagerDefault | |
@end | |
@interface DDLogInit : NSObject | |
+(void)init; | |
@end | |
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DDLogInit.m | |
// | |
// | |
#import "DDLogInit.h" | |
@implementation MyCustomFormatter | |
- (id)init { | |
if((self = [super init])) { | |
threadUnsafeDateFormatter = [[NSDateFormatter alloc] init]; | |
[threadUnsafeDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"]; | |
} | |
return self; | |
} | |
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage { | |
NSString *logLevel; | |
switch (logMessage->_flag) { | |
case DDLogFlagError : logLevel = @"ERROR"; break; | |
case DDLogFlagWarning : logLevel = @"WARINING"; break; | |
case DDLogFlagInfo : logLevel = @"INFO"; break; | |
case DDLogFlagDebug : logLevel = @"DEBUG"; break; | |
default : logLevel = @"VERBOSE"; break; | |
} | |
NSString *dateAndTime = [threadUnsafeDateFormatter stringFromDate:(logMessage->_timestamp)]; | |
NSString *logMsg = logMessage->_message; | |
return [NSString stringWithFormat:@"[%@ %@] %@", dateAndTime, logLevel, logMsg]; | |
} | |
- (void)didAddToLogger:(id <DDLogger>)logger { | |
loggerCount++; | |
NSAssert(loggerCount <= 1, @"This logger isn't thread-safe"); | |
} | |
- (void)willRemoveFromLogger:(id <DDLogger>)logger { | |
loggerCount--; | |
} | |
@end | |
@implementation MyLogFileManager | |
-(NSString *)newLogFileName { | |
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]; | |
//NSString *timeStamp = [self getTimestamp]; | |
//return [NSString stringWithFormat:@"%@.log", appName, timeStamp]; | |
return [NSString stringWithFormat:@"%@.log", appName]; | |
} | |
-(BOOL)isLogFile:(NSString *)fileName { | |
return YES; | |
} | |
-(NSString *)getTimestamp { | |
static dispatch_once_t onceToken; | |
static NSDateFormatter *dateFormatter; | |
dispatch_once(&onceToken, ^{ | |
dateFormatter = [NSDateFormatter new]; | |
[dateFormatter setDateFormat:@"YYYY.MM.dd-HH.mm.ss"]; | |
}); | |
return [dateFormatter stringFromDate:NSDate.date]; | |
} | |
@end | |
@implementation DDLogInit | |
+(void)init | |
{ | |
[DDLog addLogger:[DDOSLogger sharedInstance]]; | |
MyLogFileManager* logFileManager = [[MyLogFileManager alloc] initWithLogsDirectory:@"/var/mobile/Library/Preferences/ddlog"]; | |
//MyLogFileManager* logFileManager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:@"/var/mobile/Library/Preferences/ddlog"]; | |
//lslogFileManager.maximumNumberOfLogFiles = 1; | |
DDFileLogger *fileLogger = [[DDFileLogger alloc] initWithLogFileManager:logFileManager]; | |
fileLogger.logFormatter = [[MyCustomFormatter alloc] init]; | |
fileLogger.rollingFrequency = 60 * 60 * 24 * 300; | |
fileLogger.maximumFileSize = 1024 * 1024 * 10; | |
[DDLog addLogger:fileLogger]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment