不管哪種方法,都必須在PCH文件中做下宏定義
DEBUG和RELEASE要分開,RELEASE時log打印要取消
方法一:簡單直接,用幾行代碼搞定,簡潔但功能少
#ifdef DEBUG #define NSLog(...) NSLog(__VA_ARGS__) #define debugMethod() NSLog(@"%s", __func__) #else #define NSLog(...) #define debugMethod() #endif
這個DEBUG在哪設置呢,
在 "Target > Build Settings > Preprocessor Macros > Debug" 里有一個"DEBUG=1"。
設置為Debug模式下,Product-->Scheme-->SchemeEdit Scheme#ifdef DEBUG #define **Log( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )//分別是方法地址,文件名,在文件的第幾行,自定義輸出內容 #else #define **Log( s, ... ) #endif
其中:**Log( s, ... )的**是隨便你自定義的名字,方便代碼直接拷走使用。
1.下載框架
// 讓控制台可以輸出顏色插件
https://github.com/robbiehanson/XcodeColors
// 帶色彩日志框架
https://github.com/CocoaLumberjack/CocoaLumberjack
2.安裝XcodeColors(輸出顏色插件)
3.導入色彩日志框架
===========================================================
1.定義日志級別
#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_OFF;
#endif
2. 在didFinishLaunchingWithOptions方法中初始化帶色彩日志
[DDLog addLogger:[DDTTYLogger sharedInstance]];
3.開啟色彩日志
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
4.使用帶色彩日志
5.修復Xcode6不顯示色彩日志問題
>In Xcode bring up the Scheme Editor (Product -> Edit Scheme...)
>Select "Run" (on the left), and then the "Arguments" tab
>Add a new Environment Variable named "XcodeColors", with a value of "YES"
===========================================================
1.日志類型
DDLog:基礎類,必須引入的。
DDASLLogger:支持將調試語句寫入到蘋果的日志中。一般正對Mac開發。可選。
DDTTYLogger:支持將調試語句寫入xCode控制台。我們即使要用它。可選。
DDFileLogger:支持將調試語句寫入到文件系統。可選。
2.DDLog日志種類。
DDLogError:定義輸出錯誤文本
DDLogWarn:定義輸出警告文本
DDLogInfo:定義輸出信息文本
DDLogDebug:定義輸出調試文本
DDLogVerbose:定義輸出詳細文本
3.日志級別
>LOG_LEVEL_ERROR,那么你只會看到DDlogError語句。
>LOG_LEVEL_WARN,那么你只會看到DDLogError和DDLogWarn語句。
>LOG_LEVEL_INFO,那么你會看到error、Warn和Info語句。
>LOG_LEVEL_VERBOSE,那么你會看到所有DDLog語句。
>LOG_LEVEL_OFF,你將不會看到任何DDLog語句。
===========================================================
1.自定義顏色
[[DDTTYLogger sharedInstance] setForegroundColor:[UIColor greenColor] backgroundColor:[UIColor purpleColor] forFlag:DDLogFlagInfo];
// 快速定位打印方法
#define DDInfoLog DDLogWarn(@"%d %s", __LINE__ ,__func__)
