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日誌分類的內容介紹完了,希望本文對你有所協助!