IOS應(yīng)用中使用SimpleLogger日志分類
IOS應(yīng)用中使用SimpleLogger日志分類是本文要介紹的內(nèi)容,主要實(shí)現(xiàn)IOS中的日志分類一個(gè)實(shí)例,那么來看詳細(xì)內(nèi)容。
在壇子里看到一篇IOS中日志管理的內(nèi)容,與友們來分享一下。之前做java的時(shí)候一直用Log4j做日志的分類,但是現(xiàn)在做iphone有一段時(shí)間了,一直用NSLog做日志,但是我們?cè)陂_發(fā)過程中需要一些強(qiáng)大的日志功能,例如對(duì)日志level的控制,對(duì)行號(hào)和文件名的打印等等。有一個(gè)開源的Log4Cocoa。
學(xué)習(xí)Object-C 和 iPhone也有將近兩個(gè)月了,幾乎任何講Object-C的書第一章就會(huì)用到NSLog這個(gè)函數(shù),這個(gè)函數(shù)可以向Console輸出一些信息,方便我們跟蹤程序的運(yùn)行過程??墒俏以谧鲆恍﹊Phone的開發(fā)的時(shí)候,卻需要一些稍微強(qiáng)大的日志功能,譬如文件名,行號(hào),對(duì)一些日志Level的控制。我在Google上找了一下,有個(gè)Log4Cocoa的,好像是想做成Log4j的功能??墒俏移綍r(shí)的需求不需要那么強(qiáng)大,而且我很不喜歡殺雞用牛刀,于是我自己寫了一個(gè)簡(jiǎn)單的日志庫SimpleLogger。
其實(shí)這個(gè)不能算庫,說白了就是SimpleLogger.h和SimpleLogger.m兩個(gè)文件,夠簡(jiǎn)單吧。我定義了一些常用的宏,譬如DEBUG, ENTER, RETURN,大家可以看源代碼,也可以直接看MyLogger.m的示例,就知道怎么用了。這個(gè)日志庫可以支持iPhone和MacOSX的開發(fā),不過它不是線程安全的(iPhone沒有這個(gè)問題)。
[使用方法]
先看看下面的代碼:
- #import <Foundation/Foundation.h>
- #import "SimpleLogger.h"
- int testLogger()
- {
- ENTER(@"testLogger()");
- int rst = 10;
- RETURN(-rst, @"%d", -rst);
- }
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- [SimpleLogger getLogger];
- //insert code here
- int i = 10;
- INFO(@"i is %d", i);
- i = -100;
- INFO(@"i is %d", i);
- testLogger();
- [pool drain];
- [[SimpleLogger getLogger]release];
- return 0;
- }
- #import <Foundation/Foundation.h>
- #import "SimpleLogger.h"
- int testLogger()
- {
- ENTER(@"testLogger()");
- int rst = 10;
- RETURN(-rst, @"%d", -rst);
- }
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- [SimpleLogger getLogger];
- //insert code here
- int i = 10;
- INFO(@"i is %d", i);
- i = -100;
- INFO(@"i is %d", i);
- testLogger();
- [pool drain];
- [[SimpleLogger getLogger]release];
- return 0;
- }
使用方法也非常簡(jiǎn)單
(1)把SimpleLogger.h和SimpleLogger.m加到你的項(xiàng)目中
(2)調(diào)用[[SimpleLogger getLogger]setLogLevelSetting:SOME_LEGEL];(可選的,默認(rèn)是SLLE_MAJOR)
(3)最后調(diào)用[[SimpleLogger getLogger]release]
(4)常用方法:
- ENTER(@"method name");
- INFO(@"The count of array is %d", [array count]);
- DEBUG(@"The person's name is %@", person.name);
- ERROR(@"Impossible get into this branch");
- RETURN(rst, @"%d", rst); //rst就是返回值
- LOG(SLL_DETAILED, @"This log is very detailed with value %d", value);
- [[SimpleLogger getLogger]setLogLevelSetting:SLLS_MINOR]; //設(shè)置日志級(jí)別
下載類庫:http://wangjun.easymorse.com/wp-content/tools/SimpleLogger.zip
MyLogger.tar:http://dl.iteye.com/topics/download/2898cb63-c4c6-3042-be73-2e173cac2a64
小結(jié):iOS應(yīng)用中使用SimpleLogger日志分類的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!