1, log输出到不同的地方
2,log定义级别,设置不同级别的输出控制开关
3,使用不同的级别日志输出接口
4,格式化输出内容
5,定时器输出日志
6,日志文件管理:打包,自动拆分成多个日志文件
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
NSString *logLevelStr = @"";
switch (logMessage.flag) {
case DDLogFlagError:
logLevelStr = @"[Error] > ";
break;
case DDLogFlagWarning:
logLevelStr = @"[Warning] > ";
break;
case DDLogFlagInfo:
logLevelStr = @"[Info] > ";
break;
case DDLogFlagDebug:
logLevelStr = @"[Debug] > ";
break;
case DDLogFlagVerbose:
logLevelStr = @"[Verbose] > ";
break;
default:
break;
}
NSString *formatStr = [NSString stringWithFormat:@"%@: %@, %@, %@ line[%ld], %@", logLevelStr, logMessage.timestamp, logMessage.threadName, logMessage.function, logMessage.line, logMessage.message];
return formatStr;
}
// 设置输出到文件
DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
// 输出文件分割规则
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
// 最多log文件数量。过期的文件被替换
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[fileLogger setLogFormatter:[[MyDDLogFormatter alloc] init]];
[DDLog addLogger:fileLogger];
// 设置输出到控制台
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
[[DDTTYLogger sharedInstance] setForegroundColor:DDMakeColor(0, 0, 255) backgroundColor:DDMakeColor(0, 0, 0) forFlag:DDLogFlagDebug];
[[DDTTYLogger sharedInstance] setLogFormatter:[[MyDDLogFormatter alloc] init]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
DDLogDebug(@"asdf:%@",@"asdf");
DDLogInfo(@"asdf:%@",@"asdf");