CocoaLumberjack配合XcodeColors实现Xcode分级日志输出

声明:原文在自己的博客,只是github服务器在墙内访问不流畅,就复制了自己的一份,放在简书了。不要说我抄袭。。。

最近被问到CocoaLumberjack的使用,发现网络上的文章写的乱且不全,就在讲解之余,顺便写了一篇基础使用。本篇只简述基本使用,基本上可以满足日常使用。若想要了解其他使用,请移步CocoaLumberjack--github

CocoaLumberjack

CocoaLumberjack是一个开源的Mac和iOS日志分级输出框架。关于CocoaLumberjack不多介绍,具体见CocoaLumberjack--github
1.开始使用:

swift:

platform :ios, '8.0'
pod 'CocoaLumberjack/Swift'
use_frameworks!
import CocoaLumberjack

application:didFinishLaunchingWithOptions:中:

DDLog.addLogger(DDTTYLogger.sharedInstance()) // Xcode控制台(NSLog)
DDLog.addLogger(DDASLLogger.sharedInstance()) //发送至苹果日志系统
let fileLogger: DDFileLogger = DDFileLogger() //日志语句写入文件
fileLogger.rollingFrequency = 60*60*24  // 24小时
fileLogger.logFileManager.maximumNumberOfLogFiles = 7
DDLog.addLogger(fileLogger)
DDLogVerbose("Verbose");
DDLogDebug("Debug");
DDLogInfo("Info");
DDLogWarn("Warn");
DDLogError("Error");

Obj-C:

platform :ios, '7.0'
pod 'CocoaLumberjack'
#import <CocoaLumberjack/CocoaLumberjack.h>

在application:didFinishLaunchingWithOptions:中:

[DDLog addLogger:[DDTTYLogger sharedInstance]]; //Xcode控制台(NSLog)
[DDLog addLogger:[DDASLLogger sharedInstance]]; //发送至苹果日志系统
DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24;  // 24小时fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLog addLogger:fileLogger];
DDLogVerbose(@"Verbose");
DDLogDebug(@"Debug");
DDLogInfo(@"Info");
DDLogWarn(@"Warn");
DDLogError(@"Error");

日志输出级别

以下以OC为例进行说明。

DDLog与NSLog语法完全相同,可以无缝切换。CocoaLumberjack分为以下几个输出级别:
DDlogError DDlogWarn DDlogInfo DDLogDebug DDlogVerbose

typedef NS_ENUM(NSUInteger, DDLogLevel){
    /**
     *  No logs
     */
    DDLogLevelOff       = 0,
    
    /**
     *  Error logs only
     */
    DDLogLevelError     = (DDLogFlagError),
    
    /**
     *  Error and warning logs
     */
    DDLogLevelWarning   = (DDLogLevelError   | DDLogFlagWarning),
    
    /**
     *  Error, warning and info logs
     */
    DDLogLevelInfo      = (DDLogLevelWarning | DDLogFlagInfo),
    
    /**
     *  Error, warning, info and debug logs
     */
    DDLogLevelDebug     = (DDLogLevelInfo    | DDLogFlagDebug),
    
    /**
     *  Error, warning, info, debug and verbose logs
     */
    DDLogLevelVerbose   = (DDLogLevelDebug   | DDLogFlagVerbose),
    
    /**
     *  All logs (1...11111)
     */
    DDLogLevelAll       = NSUIntegerMax
};

根据上边的枚举,看懂每个级别都输出什么应该没问题吧。。。

如何设置日志输出级别:

可以指定单个文件内输出级别:

static const int ddLogLevel = LOG_LEVEL_VERBOSE; 

也可以设置只在DEBUG模式输出日志:

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

设置日志颜色

虽然可以分级输出log,但是遇到log语句太多的时候,还是看起来相当的头痛,我们可以配合使用XcodeColors帮助我们实现以下彩色log效果:

图2

XcodeColors的安装:XcodeColors--github

安装好XcodeColors后,我们就可以按照自己的想法定义log语句颜色了,代码如下:

    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor whiteColor] backgroundColor:[UIColor grayColor] forFlag:DDLogFlagVerbose]; //设置文字为白色,背景为灰色。

    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor redColor] backgroundColor:[UIColor whiteColor] forFlag:DDLogFlagDebug];
    
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor cyanColor] backgroundColor:[UIColor blueColor] forFlag:DDLogFlagInfo];
    
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor lightGrayColor] backgroundColor:[UIColor orangeColor] forFlag:DDLogFlagWarning];
    
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor whiteColor] backgroundColor:[UIColor redColor] forFlag:DDLogFlagError];

以上简单几步,就可以设置分级、彩色输入log,提升开发体验,极度推荐还没有使用过的小伙伴赶快试一试。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容