CocoaLumberjack开发指南

来源:互联网 发布:linux鼠标切出来 编辑:程序博客网 时间:2024/05/29 16:58

CocoaLumberjack支持ios日志文件异步记录,日志文件覆盖保存,5级别日志记录,xcode的彩色日志,作者经常维护更新,大部分的日志都基于该架构,但存在使用极为困难的问题。现总结一下使用心得,让你从零做起也能使用CocoaLumberjack日志架构的全部功能。测试环境:xcode6.2
CocoaLumberjack的下载链接是:链接地址
xCode扩展插件XcodeColors下载链接:链接地址

CocoaLumberjack该类型日志系统的原创者,并且不断及时更新,其它的库如Lumberjack都是基于它改编而成,更新困难或根本不更新。
CocoaLumberjack的优点是支持mac工程和iOS功能,提供彩色日志,但是需要用pod install的方式导入类库,对工程导入一次更新最新的CocoaLumberjack库时只要不存在文件名的变更就不用再导入了,有后台线程异步存储日志。
建议使用CocoaLumberjack,不断完善的库才能更健壮。
DDASLLogger:支持将调试语句写入到苹果的日志中。一般正对Mac开发。可选。就是发往mac控制台的系统日志。
DDTTYLogger:支持将调试语句写入xCode控制台。我们即使要用它。可选。可以支持彩色日志,使用了它便于问题定位,很好用
DDFileLogger:支持将调试语句写入到文件系统。可选。保存日志文件的个数可以指定,超过制定个数就干掉最早的日志(覆盖写日志),日志文件文件最大尺寸可以制定,最大切换文件时间可以指定,当文件超过最大文件尺寸或最大文件切换时间到并且有新的日志文件时就重新建立一个日志文件。日志文件放在类事下面的文件夹下,该文件在iPhone中一般不能直接看到。可以通过修改文件路径和工程参数可以导出日志文件。 /var/mobile/Containers/Data/Application/CECD8580-EEE6-4582-8CC0-79EEEB06146A/Documents/Logs
CocoaLumberjack有很多使用的陷阱,不然不是使用不起来就是部分功能无效:
第一:下载并安装XcodeColors。
第二:引入第三方库CocoaLumberjack。
第三:把下载库文件夹及子文件夹的文件加入工程(CocoaLumberjack-master/Classes),删除CocoaLumberjack.swift(不删除该文件编译不通过)。
第四:编辑工程的scheme,把run中的arguments中增加XcodeColors并设置为YES
第五:在预编头文件-Prefix.pch头文件中增加包含 #import “CocoaLumberjack.h”和定义日志级别。
内容:

ifdef OBJC

#import "CocoaLumberjack.h"

endif

ifdef DEBUG

static const DDLogLevel ddLogLevel = DDLogLevelVerbose;

else

static const DDLogLevel ddLogLevel = DDLogLevelOff;

endif

第六:在AppDelegate.m或AppDelegate.mm文件中包含Common.h,在- (BOOL)application:(UIApplication)application didFinishLaunchingWithOptions:(NSDictionary )launchOptions函数中增加
[DDLog addLogger:[DDTTYLogger sharedInstance]];
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLog addLogger:fileLogger];
第七:在需要打印日志的地方增加和NSLOG同样参数的日志:
DDLogError(@”Paper jam%@”, @”dsfsd”);
DDLogWarn(@”Toner is low”);
DDLogInfo(@”Warming up printer (pre-customization)”);
DDLogVerbose(@”Intializing protcol x26 (pre-customization)”);
第八:定制不同类型的日志显示不同的颜色:
DDLogError(@”Paper jam”);
DDLogWarn(@”Toner is low”);
#if TARGET_OS_IPHONE
UIColor *pink = [UIColor colorWithRed:(255/255.0) green:(58/255.0) blue:(159/255.0) alpha:1.0];
#else
NSColor *pink = [NSColor colorWithCalibratedRed:(255/255.0) green:(58/255.0) blue:(159/255.0) alpha:1.0];
#endif
[[DDTTYLogger sharedInstance] setForegroundColor:pink backgroundColor:nil forFlag:DDLogFlagInfo];
DDLogInfo(@”Warming up printer (post-customization)”);
DDLogDebug(@”DDLogDebug”);
#if TARGET_OS_IPHONE
UIColor *gray = [UIColor grayColor];
#else
NSColor *gray = [NSColor grayColor];
#endif
[[DDTTYLogger sharedInstance] setForegroundColor:gray backgroundColor:nil forFlag:DDLogFlagVerbose];
DDLogVerbose(@”Intializing protcol x26 (post-customization)”);

CocoaLumberjack本地化增强功能:

第九:对CocoaLumberjack的功能进行扩展增加通过ituns工具对制定应用的日志文件的导出。
把DDFileLogger.m下的- (NSString *)defaultLogsDirectory函数的NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);修改为:NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);来让日志写在app的可以访问的文档目录下。
在targets的build settings中的Info.list File指向的Info.plist文件中增加一列键值 Application supports iTunes file sharing 类型是Boolean值YES.(注意发布正式版本时要把该属性设置为NO。当它的属性变更并且ituns在使用时,要打开关闭ituns后再打开再能保证ituns访问文件的权限和手机应用的权限一致。)这样就可以在手机连接到电脑上,点击iPhone,点应用程序,在文件共享中点击对应的app,点Logs文件夹,点存储到…指定文件夹就可以导出日志文件了,当文件刷新时导致看不到时,通过点击其它应用再点该应用可以重新刷新出Logs文件夹

第十:对CocoaLumberjack缺少打印日志时的文件名和行数的功能进行扩展。
通过修改你的全局头文件(如:Macro.h中的宏定义来实现,不过那样你就要使用新的打印日志函数。代码如下:

ifndef CLDDLogError

define CLDDLogError(format, …) \

{ \
DDLogError((@”%@.m:%d Err:” format), NSStringFromClass([self class]), LINE, ##VA_ARGS); \
}

endif

ifndef CLDDLogWarn

define CLDDLogWarn(format, …) \

{ \
DDLogWarn((@”%@.m:%d Warn:” format), NSStringFromClass([self class]), LINE, ##VA_ARGS); \
}

endif

ifndef CLDDLogInfo

define CLDDLogInfo(format, …) \

{ \
DDLogInfo((@”%@.m:%d Info:” format), NSStringFromClass([self class]), LINE, ##VA_ARGS); \
}

endif

ifndef CLDDLogDebug

define CLDDLogDebug(format, …) \

{ \
DDLogDebug((@”%@.m:%d Debug:” format), NSStringFromClass([self class]), LINE, ##VA_ARGS); \
}

endif

ifndef CLDDLogVerbose

define CLDDLogVerbose(format, …) \

{ \
DDLogVerbose((@”%@.m:%d Verbose:” format), NSStringFromClass([self class]), LINE, ## VA_ARGS); \
}

endif

参考内容:

一、安装xCode扩展插件XcodeColors

1.下载地址:链接地址

2.下载完成后,打开XcodeColors项目,编译即可。它会在你的电脑中安装插XcodeColors.xcplugin。具体路径在:~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeColors.xcplugin(可以去瞅一眼)。

3.退出xCode,重新打开。

4.运行TestXcodeColors项目,尝试,是否安装成功。

二、引入第三方库CocoaLumberjack。

1.下载DDLog。下载地址:链接地址

2.将DDLog添加到你的项目中。

unsigned long long const kDDDefaultLogMaxFileSize = 1024 * 1024; // 1 MB
NSTimeInterval const kDDDefaultLogRollingFrequency = 60 * 60 * 24; // 24 Hours
NSUInteger const kDDDefaultLogMaxNumLogFiles = 5; // 5 Files
unsigned long long const kDDDefaultLogFilesDiskQuota = 20 * 1024 * 1024; // 20 MB

char *xcode_colors = getenv(XCODE_COLORS);

if (xcode_colors && (strcmp(xcode_colors, “YES”) == 0))

{

// XcodeColors is installed and enabled!

}

DEMO下载地址:链接地址,请确保最新的插件XcodeColors

  1. 问题1 Demo里测试日志颜色正常,在自己的项目里就不会显示颜色

    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”

  2. 问题2 内存暴涨问题

加入CocoaLumberjack后,模拟器测试正常,真机测试内存暴涨,导致xcode自动终结联机调试,不调用DDLog相关的代码就没有问题。最终缩小范围,发现把上面的第4步中的Environment Variable给删掉就没有问题,我真是晕了。

<iframe id="cproIframe_u1764695_1" height="90" marginheight="0" src="http://pos.baidu.com/acom?adn=3&amp;at=97&amp;aurl=&amp;cad=1&amp;ccd=24&amp;cec=utf-8&amp;cfv=14&amp;ch=0&amp;col=zh-cn&amp;conOP=0&amp;cpa=1&amp;dai=1&amp;dis=0&amp;ltr=http%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3D8POZNfpKFRg5SAUxsWNtWo92T4UT5oH0EZ_7ThgARaNGQSiUIk2cSuSmAWtx7CT2pYzkIYG8q7KwGpMmAaf55_%26wd%3D%26eqid%3Df8d69f3b000248be0000000555ee748b&amp;ltu=http%3A%2F%2Fwww.aiuxian.com%2Farticle%2Fp-2471872.html&amp;lunum=6&amp;n=55019089_cpr&amp;pcs=1544x708&amp;pis=10000x10000&amp;ps=3797x253&amp;psr=1600x900&amp;pss=1544x3863&amp;qn=e0fb73e9ffd184b9&amp;rad=&amp;rsi0=728&amp;rsi1=90&amp;rsi5=4&amp;rss0=%23F1DD95&amp;rss1=%23FFFFFF&amp;rss2=%230000ff&amp;rss3=%23444444&amp;rss4=%23008000&amp;rss5=&amp;rss6=%23e10900&amp;rss7=&amp;scale=&amp;skin=&amp;td_id=1764695&amp;tn=text_default_728_90&amp;tpr=1441690795295&amp;ts=1&amp;version=2.0&amp;xuanting=0&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;di=u1764695&amp;ti=CocoaLumberjack%E7%9A%84ios%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97%20-%20%E7%88%B1%E6%82%A0%E9%97%B2%2C%E5%BF%AB%E4%B9%90%E5%B7%A5%E4%BD%9C%2C%E6%82%A0%E9%97%B2%E7%94%9F%E6%B4%BB!&amp;tt=1441690795266.29.129.129" frameborder="0" width="728" allowtransparency="true" marginwidth="0" scrolling="no" align="center,center"></iframe>
0 0
原创粉丝点击