在log4j中,在记录之前检查isDebugEnabled是否提高了性能? [英] In log4j, does checking isDebugEnabled before logging improve performance?

查看:89
本文介绍了在log4j中,在记录之前检查isDebugEnabled是否提高了性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 Log4J 进行日志记录。以前我使用调试调用如:

I am using Log4J in my application for logging. Previously I was using debug call like:

选项1:

logger.debug("some debug text");

但有些链接表明最好检查 isDebugEnabled()首先,如:

but some links suggest that it is better to check isDebugEnabled() first, like:

选项2:

boolean debugEnabled = logger.isDebugEnabled();
if (debugEnabled) {
    logger.debug("some debug text");
}

所以我的问题是选项2能否以任何方式提高性能?

因为在任何情况下Log4J框架都对debugEnabled进行相同的检查。对于选项2,如果我们在单个方法或类中使用多个调试语句可能是有益的,其中框架不需要多次调用 isDebugEnabled()方法(每个呼叫);在这种情况下,它只调用 isDebugEnabled()方法一次,如果Log4J配置为调试级别,则实际调用 isDebugEnabled()方法两次:

Because in any case Log4J framework have same check for debugEnabled. For option 2 it might be beneficial if we are using multiple debug statement in single method or class, where the framework does not need to call isDebugEnabled() method multiple times (on each call); in this case it calls isDebugEnabled() method only once, and if Log4J is configured to debug level then actually it calls isDebugEnabled() method twice:


  1. 如果为debugEnabled变量赋值,并且

  2. 实际上由logger.debug()方法调用。

我不认为如果我们编写多个记录器。 debug()语句在方法或类中,并根据选项1调用 debug()方法,然后与选项2相比,它是Log4J框架的开销。由于 isDebugEnabled()是一个非常小的方法(就代码而言),它可能是内联的好选择。

I don't think that if we write multiple logger.debug() statement in method or class and calling debug() method according to option 1 then it is overhead for Log4J framework in comparison with option 2. Since isDebugEnabled() is a very small method (in terms of code), it might be good candidate for inlining.

推荐答案

在这种特殊情况下,选项1更好。

In this particular case, Option 1 is better.

保护声明(检查 isDebugEnabled ())当涉及调用 toString时,可以防止日志消息的潜在昂贵计算( )各种对象的方法并连接结果。

The guard statement (checking isDebugEnabled()) is there to prevent potentially expensive computation of the log message when it involves invocation of the toString() methods of various objects and concatenating the results.

在给定的例子中,日志消息是一个常量字符串,所以让记录器丢弃它与检查记录器是否启用一样有效,并且它降低了代码的复杂性,因为分支较少。

In the given example, the log message is a constant string, so letting the logger discard it is just as efficient as checking whether the logger is enabled, and it lowers the complexity of the code because there are fewer branches.

更好的是使用更多的最新的日志记录框架,其中日志语句采用格式规范和由logger替换的参数列表—但懒惰,仅在启用了记录器的情况下。这是 slf4j

Better yet is to use a more up-to-date logging framework where the log statements take a format specification and a list of arguments to be substituted by the logger—but "lazily," only if the logger is enabled. This is the approach taken by slf4j.

参见我对相关问题的回答以获取更多信息,以及使用log4j执行此类操作的示例。

See my answer to a related question for more information, and an example of doing something like this with log4j.

这篇关于在log4j中,在记录之前检查isDebugEnabled是否提高了性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆