一 利用cocopods 安装CocoaLumberjack(2.2.0)
1.必须要定义 打印级别 DDLog 才能编译通过
#ifdef DEBUG
static const int ddLogLevel = DDLogLevelVerbose;
#else
static const int ddLogLevel = DDLogLevelWarning;
#endif
2.开启颜色 需要装XcodeColors 插件并
//1> 开启使用 XcodeColors
setenv("XcodeColors", "YES", 0);
//2 >检测是否开启 XcodeColors
char *xcode_colors = getenv("XcodeColors");
if (xcode_colors && (strcmp(xcode_colors, "YES") == 0) {
// XcodeColors is installed and enabled!
NSLog(@"XcodeColors is installed and enabled");
}
//3 >开启DDLog 颜色
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor blueColor] backgroundColor:nil forFlag:DDLogFlagVerbose];
3.DDLogError 和 NSLog 一样 都是同步打印,其他的。。。。没理解还
NSLog(@"NSLog");
DDLogVerbose(@"Verbose");
DDLogDebug(@"Debug");
DDLogInfo(@"Info");
DDLogWarn(@"Warn");
DDLogError(@"Error");
4.DDFileLogger 打印的文件路径
NSHomeDirectory()/Library/Caches/Logs/com.jaga.test 2016-01-22 03-35.log
5.上代码
//!!DDLog 必须配置打印级别
#ifdef DEBUG
static const int ddLogLevel = DDLogLevelVerbose;
#else
static const int ddLogLevel = DDLogLevelWarning;
#endif
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//开启使用 XcodeColors
setenv("XcodeColors", "YES", 0);
//检测
char *xcode_colors = getenv("XcodeColors");
if (xcode_colors && (strcmp(xcode_colors, "YES") == 0))
{
// XcodeColors is installed and enabled!
NSLog(@"XcodeColors is installed and enabled");
}
//开启DDLog 颜色
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor blueColor] backgroundColor:nil forFlag:DDLogFlagVerbose];
//配置DDLog
[DDLog addLogger:[DDTTYLogger sharedInstance]]; // TTY = Xcode console
[DDLog addLogger:[DDASLLogger sharedInstance]]; // ASL = Apple System Logs
DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLog addLogger:fileLogger];
//针对单个文件配置DDLog打印级别,尚未测试
// [DDLog setLevel:DDLogLevelAll forClass:nil];
NSLog(@"NSLog");
DDLogVerbose(@"Verbose");
DDLogDebug(@"Debug");
DDLogInfo(@"Info");
DDLogWarn(@"Warn");
DDLogError(@"Error");
DDLogError(NSHomeDirectory());
return YES;
}