NSLog 就一般我們在AS3的trace()一樣,用在追蹤一些變數或function執行的前後順序,而Lumberjack 除了用來追蹤,另外還可以Filter一些訊息,如 DDLogError 的功能,只有在產生Error 的時候,才會 Print 錯誤訊息出來,沒有Error 的時候,並不會執行。
目前Lumberjack 分成幾個level,如下:
- DDLogError–只會看到 Error 的訊息
- DDLogWarn–只會看到 Error,warn的訊息
- DDLogInfo—-只會看到 Error ,warn,Info 的訊息
- DDLogVerbose–會看到全部的訊息
下載網址:https://github.com/robbiehanson/CocoaLumberjack
1.將 Lumberjack 套件增加到專案中。
2.如果不使用ARC的機制,必須要在Build Phases中,找到Lumberjack的檔案,如DDLog.m,DDFileLogger.m…等等。
3.double click,加上「-fobjc-arc」
![Build Phases 參考圖](http://ikevin.tw/wp-content/uploads/2012/07/xxx.jpg)
4.在AppDelegate.m 中
#import "AppDelegate.h"
#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDTTYLogger.h"
@implementation AppDelegate
#pragma mark - define
#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_WARN;
#endif
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
// setup logging framework
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
[self.window makeKeyAndVisible];
return YES;
}
以上四個步驟應該就可以使用 Lumberjack 的log 機制了,比如說在RootViewController.m 裡,為每個 function 都下DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD),這樣我們就可追蹤整個城市的流程了。
#import "RootViewController.h"
#import "DDLog.h"
#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_WARN;
#endif
- (void)viewDidLoad
{
[super viewDidLoad];
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}