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

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

问题描述

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

选项 1:

logger.debug("一些调试文本");

但有些链接建议最好先检查 isDebugEnabled(),例如:

选项 2:

boolean debugEnabled = logger.isDebugEnabled();如果(调试启用){logger.debug("一些调试文本");}

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

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

  1. 如果为 debugEnabled 变量赋值,以及
  2. 实际上由 logger.debug() 方法调用.

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

解决方案

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

guard 语句(检查 isDebugEnabled())是为了防止在涉及调用各种对象的 toString() 方法时对日志消息进行潜在的昂贵计算并连接结果.

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

更好的是使用更新的日志框架,其中日志语句采用格式规范和要由记录器替换的参数列表—但懒惰",仅当记录器启用时.这是 slf4j.

查看我对相关问题的回答了解更多信息,以及使用 log4j 执行此类操作的示例.

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

Option 1:

logger.debug("some debug text");

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

Option 2:

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

So my question is "Does option 2 improve performance any way?".

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. In case of assigning value to debugEnabled variable, and
  2. Actually called by logger.debug() method.

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.

解决方案

In this particular case, Option 1 is better.

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.

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.

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天全站免登陆