IOS應用中使用SimpleLogger日志分類
IOS應用中使用SimpleLogger日志分類是本文要介紹的內容,主要實現IOS中的日志分類一個實例,那么來看詳細內容。
在壇子里看到一篇IOS中日志管理的內容,與友們來分享一下。之前做java的時候一直用Log4j做日志的分類,但是現在做iphone有一段時間了,一直用NSLog做日志,但是我們在開發過程中需要一些強大的日志功能,例如對日志level的控制,對行號和文件名的打印等等。有一個開源的Log4Cocoa。
學習Object-C 和 iPhone也有將近兩個月了,幾乎任何講Object-C的書第一章就會用到NSLog這個函數,這個函數可以向Console輸出一些信息,方便我們跟蹤程序的運行過程。可是我在做一些iPhone的開發的時候,卻需要一些稍微強大的日志功能,譬如文件名,行號,對一些日志Level的控制。我在Google上找了一下,有個Log4Cocoa的,好像是想做成Log4j的功能。可是我平時的需求不需要那么強大,而且我很不喜歡殺雞用牛刀,于是我自己寫了一個簡單的日志庫SimpleLogger。
其實這個不能算庫,說白了就是SimpleLogger.h和SimpleLogger.m兩個文件,夠簡單吧。我定義了一些常用的宏,譬如DEBUG, ENTER, RETURN,大家可以看源代碼,也可以直接看MyLogger.m的示例,就知道怎么用了。這個日志庫可以支持iPhone和MacOSX的開發,不過它不是線程安全的(iPhone沒有這個問題)。
[使用方法]
先看看下面的代碼:
- #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;
- }
使用方法也非常簡單
(1)把SimpleLogger.h和SimpleLogger.m加到你的項目中
(2)調用[[SimpleLogger getLogger]setLogLevelSetting:SOME_LEGEL];(可選的,默認是SLLE_MAJOR)
(3)最后調用[[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]; //設置日志級別
下載類庫:http://wangjun.easymorse.com/wp-content/tools/SimpleLogger.zip
MyLogger.tar:http://dl.iteye.com/topics/download/2898cb63-c4c6-3042-be73-2e173cac2a64
小結:iOS應用中使用SimpleLogger日志分類的內容介紹完了,希望本文對你有所幫助!