java 日志调试启用检查java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10428447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:00:12  来源:igfitidea点击:

log debug enabled check in java

javalogginglog4j

提问by user2434

What is the point of having the if statement here? The severity level can be changed in log4j.xml config file. If the severity level is debug, it'll log the debug message, else it will not.

在这里使用 if 语句有什么意义?严重性级别可以在 log4j.xml 配置文件中更改。如果严重级别是调试,它会记录调试消息,否则不会。

What is the significance of the if statement below?

下面的if语句有什么意义?

   if (log.isDebugEnabled())
    {
        log.debug("I am logging something");
    }

回答by Sridhar G

In your example there is no need for an 'if' statement (between 'if' is not a loop)

在您的示例中,不需要“if”语句(“if”之间不是循环)

But if you take the below example then you can see that we do a concatenation and if the log level is info then unneccesarily this concatenation operation will be done. For better performance we check this condition.

但是如果你看下面的例子,那么你可以看到我们做了一个连接,如果日志级别是信息,那么这个连接操作将不必要地完成。为了获得更好的性能,我们检查此条件。

if (log.isDebugEnabled())
{
    log.debug("I am logging " + 1234 + ".");
}

Extra info:

额外信息:

Use slf4j to avoid such if conditions. The above statement can be rewritten in slf4j as follows,

使用 slf4j 来避免这种 if 条件。上面的语句可以在slf4j中改写如下,

log.debug("I am logging {} .", 1234);

回答by Betlista

This is considered as good practice. For example if there is some string concatenation it's not evaluated and checked in log4j, but it is checked first.

这被视为良好做法。例如,如果有一些字符串连接,它不会在 log4j 中进行评估和检查,而是首先检查。

Example:

例子:

if ( log.isDebugEnabled() ) {
    log.debug( "N=" + N + ", a=" + Arrays.toString( a ) );
}

method Arrays.toString()and also concatenation is not performed if debug is not enabled. If there is no ifit is invoked first and checked later, that's all ;-)

Arrays.toString()如果未启用调试,则不会执行方法和连接。如果没有if,则先调用它,然后再检查,仅此而已;-)

My opinion is that when there is simple String as in your example the if around the logging is not needed, if there is something more complicated (even more complicated as in my example) this can save some CPU time in production mode (without debug mode enabled).

我的意见是,当您的示例中有简单的 String 时,不需要 if 周围的日志记录,如果有更复杂的东西(在我的示例中甚至更复杂),这可以在生产模式下节省一些 CPU 时间(没有调试模式)启用)。

You have to realize also that when in case of concatenation there is String.valuOf()call which (for not null objects) calls toString()method, which can be really performance issue for big data objects (beans with many properties) if you consider that it's invoking no business logic (therefore it is "useless").

您还必须意识到,在连接的情况下,String.valuOf()调用 which(对于非空对象)调用toString()方法,如果您认为它没有调用业务逻辑,这对于大数据对象(具有许多属性的 bean)来说可能是真正的性能问题(因此它是“无用的”)。

回答by harto

It's a performance optimisation. It's done to avoid evaluating the arguments to log.debug. It's only worth doing if constructing the log message is particularly expensive (e.g. serialising an XML document).

这是性能优化。这样做是为了避免评估 的参数log.debug。只有在构建日志消息特别昂贵(例如序列化 XML 文档)时才值得这样做。

This is covered in some detail in the Short introduction to log4j.

log4j简短介绍中对此进行了详细介绍