您的位置:首页 > 移动开发 > IOS开发

iOS开发, 关于nslog的几种用法

2015-12-15 17:45 453 查看
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

设置Build Configuration成Debug时,就可以打印nslog了。

设置Release,发布app版本的时候就不会打印了,提高了性能

或者

#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, ... )的**是随便你自定义的名字,方便代码直接拷走使用。

方法二:第三方,多彩log,功能多,但稍复杂

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__)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: