在使用logger.debug()之前是否需要Log4j isDebugEnabled()? [英] Is Log4j isDebugEnabled() necessary before using logger.debug()?
问题描述
当我浏览一些代码时,我注意到使用logger如下,
When I was going through some code, I noticed the use of logger as follows,
if(logger.isDebugEnabled())
logger.debug("Something..");
但在某些代码中,我观察过这样。
But in some codes, I observed like this.
logger.debug("Something..");
当我查看log4j的源代码时,在 debug()
Logger本身的方法 if(logger.isDebugEnabled())
已被选中。那么为什么我们需要这种不必要的开销 if(logger.isDebugEnabled())
??
When I looked at the source of log4j, in the debug()
method of Logger itself if(logger.isDebugEnabled())
was checked. Then why do we need this unnecessary overhead if(logger.isDebugEnabled())
??
推荐答案
当传递给 logger.debug(...)
的String需要时间来评估时很有用,在这种情况下,如果调试你可以跳过这个评估未启用。
It's useful when the String your passing to logger.debug(...)
takes time to evaluate, in that case you can skip this evaluation if debug is not enabled.
if(logger.isDebugEnabled()) {
logger.debug("The meaning of life is " + calculateMeaningOfLife());
}
IMO这使得代码的可读性低得多,因此只应在有显着的性能提升。
IMO this makes the code a lot less readable so it should only be used when there's a significant performance improvement.
这篇关于在使用logger.debug()之前是否需要Log4j isDebugEnabled()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!