利用CocoaLumberjack框架+XcodeColors插件,调试输出有彩色的信息

效果如下:

 

步骤:

1. 安装Xcode插件:XcodeColors(方法请参考这里

2. 为项目添加 CocoaLumberjack 框架(方法请参考这里

3. 添加代码

(1) 为项目添加 pch 文件,比如文件名为 PrefixHeader.pch

内容如:

#ifndef <你的项目名>_PrefixHeader_pch
#define <你的项目名>_PrefixHeader_pch

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "DDLog.h"
#endif

#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_OFF;
#endif

#endif

 

(2) 修改项目 Build Settings 页,更改左上角的 Basic 为 All,在右侧搜索栏里输入“prefix”,快速找到 Apple LLVM 6.x - Language 下面的 Prefix Header

修改值为“项目目录名/PrefixHeader.pch”(这是一个相对路径,必须以项目目录名开头)

(3) 在 AppDelegate.m 的“- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions”里

加入代码:(需要加入头“#import "CocoaLumberjack.h"”)

// Enable XcodeColors
setenv("XcodeColors", "YES", 0);

// Standard lumberjack initialization
[DDLog addLogger:[DDTTYLogger sharedInstance]];

// And then enable colors
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];

 

(4) 在需要调用输出的地方用这样的代码:(图中的例子是写在Button的点击功能里)

DDLogError(@"DDLogError");  // Red
DDLogWarn(@"DDLogWarn");   // Orange
DDLogInfo(@"DDLogInfo");  // Default (black)
DDLogVerbose(@"DDLogVerbose");  // Default (black)

 

推荐阅读:

iOS开源项目之日志框架CocoaLumberjack

posted @ 2015-08-10 18:15  Bob-wei  阅读(508)  评论(0编辑  收藏  举报