0

我真的不明白日志级别是什么意思。

在 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

那些是什么意思?我在项目中搜索了ddLogLevelvar,但没有发现它在任何地方使用。也,不是在伐木工人的吊舱里。

4

2 回答 2

2

设置ddLogLevel过滤器从各种DDLogXXX方法中显示哪些消息。

如果设置ddLogLevel为,LOG_LEVEL_ALL则将记录所有DDLogXXX方法。如果您设置ddLogLevel为,LOG_LEVEL_INFO则只有InfoWarningError将被记录。

只需查看#define您显示的行列表。选择给定值只会产生来自该级别的消息以及列表中更高级别的消息。

如果您设置ddLogLevelLOG_LEVEL_INFO并且您有以下两行:

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

然后只记录第一条消息,因为Debug低于Info.

每个级别的实际含义有些主观。只需在您的应用程序中始终如一地使用它们。最重要或关键的消息应具有最高级别,最不重要的消息应具有较低级别。

DDLogError当我的应用程序遇到意外值或提供NSError参数的方法失败时,我会使用它。我记录了一条相关消息并包含该NSError值。

DDLogInfo用于“我在这里”类型的消息。

DDLogDebug用来记录变量值。

我不DDLogWarn经常使用,但您可以将它用于没有实际错误但需要注意的重要问题的意外问题。

于 2016-11-11T21:27:29.233 回答
2

这些是不同程度的日志记录粒度。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常量是您如何确定所需的日志记录级别。

于 2016-11-11T21:27:47.187 回答