簡體   English   中英

CocoaLumberjack日志級別

[英]CocoaLumberjack log level

我不太了解日志級別的含義。

在Lumbejack中,定義了以下日志級別:

#define LOG_LEVEL_OFF     DDLogLevelOff
#define LOG_LEVEL_ERROR   DDLogLevelError
#define LOG_LEVEL_WARN    DDLogLevelWarning
#define LOG_LEVEL_INFO    DDLogLevelInfo
#define LOG_LEVEL_DEBUG   DDLogLevelDebug
#define LOG_LEVEL_VERBOSE DDLogLevelVerbose
#define LOG_LEVEL_ALL     DDLogLevelAll

這些是什么意思? 以及如何使用它們? 與CocoaLumberjack相關的都是iOS版嗎?

另外,我在pch文件中使用以下代碼:

#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_ERROR;
#endif

那是什么意思 我在項目中搜索了ddLogLevel var,但沒有在任何地方使用它。 另外,不要在伐木工人豆莢中。

設置ddLogLevel過濾來自各種DDLogXXX方法的消息。

如果將ddLogLevel設置為LOG_LEVEL_ALL則將記錄所有DDLogXXX方法。 如果將ddLogLevel設置為LOG_LEVEL_INFO則僅記錄InfoWarningError

只需查看顯示的#define行列表即可。 選擇給定值只會導致該級別的消息以及列表中較高級別的消息。

如果將ddLogLevel設置為LOG_LEVEL_INFO ,則有以下兩行:

DDLogInfo("some info message");
DDLogDebug("some debug message");

然后,由於Debug低於Info因此僅記錄第一條消息。

每個級別的實際含義都有些主觀。 只需在您的應用中一致地使用它們即可。 最重要或最重要的消息應具有最高級別,而最不重要的消息應具有較低級別。

當我的應用遇到意外值或提供NSError參數的方法失敗時,我使用DDLogError 我記錄了一條相關消息,並包含NSError值。

我將DDLogInfo用於“我在這里”類型的消息。

我使用DDLogDebug記錄變量值。

我不經常使用DDLogWarn但是您可以將其用於沒有實際錯誤但需要注意的意外問題。

這些是不同程度的日志記錄粒度。 LOG_LEVEL_ALL表示將任何日志寫入伐木工人使用的控制台和文件。 LOG_LEVEL_OFF是沒有日志記錄的極端的另一端。 您可以使用它們來確定要顯示哪種版本的日志。 以下是一些用例示例。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // this log isn't particularly useful for a production build, 
    // but can be helpful when debugging. So we use verbose.
    DDLogVerbose(@"This view controller loaded");
}

- (void)didRecieveMemoryWarning
{
    // This is a bad situation, but it isn't an error really.
    // Just a signal that we need to free up memory. So we use warning.
    DDLogWarning(@"We are using too much memory.");
}

- (void)doSomething
{
    NSError *error;
    [self doSomethingElse:&error];
    if (error) {
        // This is definitely an error we need to handle. If you have to
        // look through crash logs on a production build, you definitely
        // want this log to show up there.
        DDLogError(@"Encountered an error: %@", error);
    }
}

在這些示例中,當您從Xcode運行應用程序時,將顯示所有日志,但是在生產崩潰日志中將僅顯示錯誤日志。 ddLogLevel常數是您確定所需的日志記錄級別的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM