DDlog庫可以讓Xcode的輸出臺變得五彩繽紛,同時可以控制打印的開關.對於調試很是方便.
下面介紹一下安裝方法.
一、安裝xCode擴展插件XcodeColors
1.下載地址:https://github.com/robbiehanson/XcodeColors
2.下載完成後,打開XcodeColors項目,編譯即可。
3.完全退出xCode,重新打開。
4.運行TestXcodeColors項目,嘗試,是否安裝成功。
二、引入第三方庫CocoaLumberjack。
1.下載DDLog。下載地址:https://github.com/CocoaLumberjack/CocoaLumberjack
2.將DDLog添加到你的項目中。 或者使用cocoapod: pod 'CocoaLumberjack', '~> 2.0.0-beta4'
三、DDLog常用API介紹
1.初始化DDLog。
DDLog:基礎類,必須引入的。
DDASLLogger:支持將調試語句寫入到蘋果的日誌中。一般正對Mac開發。可選。
DDTTYLogger:支持將調試語句寫入xCode控制檯。我們即使要用它。可選。
DDFileLogger:支持將調試語句寫入到文件系統。可選。
2.DDLog日誌種類。
DDLog的輸出級別默認提供以下若干種:
DDLogError:定義輸出錯誤文本
DDLogWarn:定義輸出警告文本
DDLogInfo:定義輸出信息文本
DDLogDebug:定義輸出調試文本
DDLogVerbose:定義輸出詳細文本
提供的日誌級別爲:
LOG_LEVEL_ERROR:只顯示錯誤日誌。
LOG_LEVEL_WARN:包括:LOG_LEVEL_ERROR
LOG_LEVEL_INFO:包括:LOG_LEVEL_WARN
LOG_LEVEL_DEBUG:包括:LOG_LEVEL_INFO
LOG_LEVEL_VERBOSE:包括:LOG_LEVEL_DEBUG
LOG_LEVEL_OFF:關閉日誌
3.使用DDLog.
<span style="font-size:14px;">static const int ddLogLevel = LOG_LEVEL_VERBOSE;// 定義日誌級別
[DDLog addLogger:[DDTTYLogger sharedInstance]];// 初始化DDLog日誌輸出,在這裏,我們僅僅希望在xCode控制檯輸出
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];// 啓用顏色區分
DDLogError(@"錯誤信息"); // 紅色
DDLogWarn(@"警告"); // 橙色
DDLogInfo(@"提示信息"); // 默認是黑色
DDLogVerbose(@"詳細信息"); // 默認是黑色
DDLog同時支持自定義日誌的顏色.
[DDLog addLogger:[DDASLLogger sharedInstance]];// 如果需要,可以添加其他的日誌輸出支持
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor blueColor]backgroundColor:nil forFlag:LOG_FLAG_INFO];// 修改指定打印類型的顏色</span>
當你在一個頁面中使用DDLog時,需要在這個頁面中申明: staticconst int ddLogLevel =LOG_LEVEL_VERBOSE; 注意ddLogLevel名字不可自定義.
提示:
如果按上面的步驟執行後發現在Xcode輸出臺中任然沒有顏色.則需要按以下步驟調節:
Select the "Run" -> "Arguments"
Under "Environment Variables"
Click the "+" button.
In the "Name" column enter: "XcodeColors"
In the "Value" column enter: "YES"
OR:在APPDelegate方法中:
#if TARGET_IPHONE_SIMULATOR
// Sends log statements to Xcode console - if available
setenv("XcodeColors", "YES", 1);
[DDLog addLogger:[DDTTYLogger sharedInstance]];
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
#endif