IOS應用中使用SimpleLogger日誌分類

來源:互聯網
上載者:User

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沒有這個問題)。

[使用方法]    

先看看下面的代碼:

 
  1. #import <Foundation/Foundation.h>     
  2. #import "SimpleLogger.h"     
  3.     
  4. int testLogger()     
  5. {     
  6.     ENTER(@"testLogger()");     
  7.     int rst = 10;     
  8.     RETURN(-rst, @"%d", -rst);     
  9. }     
  10.     
  11. int main (int argc, const char * argv[]) {     
  12.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];     
  13.     [SimpleLogger getLogger];     
  14.          
  15.     //insert code here     
  16.     int i = 10;     
  17.     INFO(@"i is %d", i);     
  18.     i = -100;     
  19.     INFO(@"i is %d", i);            
  20.     testLogger();     
  21.     [pool drain];     
  22.     [[SimpleLogger getLogger]release];     
  23.     return 0;      
  24. }    
  25.  
  26. #import <Foundation/Foundation.h> 
  27. #import "SimpleLogger.h"  
  28.  
  29. int testLogger()  
  30. {  
  31.     ENTER(@"testLogger()");  
  32.     int rst = 10;  
  33.     RETURN(-rst, @"%d", -rst);  
  34. }  
  35.  
  36. int main (int argc, const char * argv[]) {  
  37.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  38.     [SimpleLogger getLogger];  
  39.       
  40.     //insert code here  
  41.     int i = 10;  
  42.     INFO(@"i is %d", i);  
  43.     i = -100;  
  44.     INFO(@"i is %d", i);         
  45.     testLogger();  
  46.     [pool drain];  
  47.     [[SimpleLogger getLogger]release];  
  48.     return 0;   

使用方法也非常簡單

1)把SimpleLogger.h和SimpleLogger.m加到你的項目中

2)調用[[SimpleLogger getLogger]setLogLevelSetting:SOME_LEGEL];可選的,預設是SLLE_MAJOR)

3)最後調用[[SimpleLogger getLogger]release]

4)常用方法:

 
  1. ENTER(@"method name");       
  2. INFO(@"The count of array is %d", [array count]);       
  3. DEBUG(@"The person's name is %@", person.name);       
  4. ERROR(@"Impossible get into this branch");       
  5. RETURN(rst, @"%d", rst); //rst就是傳回值      
  6.  LOG(SLL_DETAILED, @"This log is very detailed with value %d", value);       
  7.  [[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日誌分類的內容介紹完了,希望本文對你有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.