CocoaLumberjack可以帶顏色Log,具體的好處嘛,誰用誰知道,:]
具體步驟如下:
1. 安裝XcodeColors插件
下載地址:https://github.com/robbiehanson/XcodeColors
安裝方法:
- 下載並解壓縮XcodeColors-master.zip
- 打開XcodeColors項目,編譯項目可以自動將插件安裝至~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeColors.xcplugin
- 重新啟動Xcode
- 再次打開XcodeColors項目
- 運行TestXcodeColors測試插件是否安裝成功
2. 下載CocoaLumberjack開源框架
下載地址:https://github.com/CocoaLumberjack/CocoaLumberjack
3. 新建項目,將CocoaLumberjack拖入項目中
4. 創建Common.h
#ifdef DEBUG static const int ddLogLevel = LOG_LEVEL_VERBOSE; #else static const int ddLogLevel = LOG_LEVEL_OFF; #endif
5. 在xxx-Prefix.pch中添加Common.h的引入
#import "Common.h"
6. 實例化DDLog
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中設置DDLog
// 實例化 lumberjack [DDLog addLogger:[DDTTYLogger sharedInstance]]; // 允許顏色 [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
使用方法
lumberjack提供了四種Log方法
DDLogError(@"錯誤信息"); // 紅色 DDLogWarn(@"警告"); // 橙色 DDLogInfo(@"提示信息"); // 默認是黑色 DDLogVerbose(@"詳細信息"); // 默認是黑色
其他
如果要修改Log輸出的顏色可以使用如下代碼:
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor blueColor] backgroundColor:nil forFlag:LOG_FLAG_INFO];