NSLog-----CocoaLumberjack

NSLog 就一般我們在AS3的trace()一樣,用在追蹤一些變數或function執行的前後順序,而Lumberjack 除了用來追蹤,另外還可以Filter一些訊息,如 DDLogError 的功能,只有在產生Error 的時候,才會 Print 錯誤訊息出來,沒有Error 的時候,並不會執行。

目前Lumberjack 分成幾個level,如下:

  1. DDLogError–只會看到 Error 的訊息
  2. DDLogWarn–只會看到 Error,warn的訊息
  3. DDLogInfo—-只會看到 Error ,warn,Info 的訊息
  4. DDLogVerbose–會看到全部的訊息

下載網址:https://github.com/robbiehanson/CocoaLumberjack

1.將 Lumberjack 套件增加到專案中。
2.如果不使用ARC的機制,必須要在Build Phases中,找到Lumberjack的檔案,如DDLog.m,DDFileLogger.m…等等。
3.double click,加上「-fobjc-arc」

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);   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值