简短版:您也可以进行布尔isDebugEnabled()检查。
原因:
1-如果逻辑复杂/字符串连接不正确。被添加到您的调试语句中,您将已经进行了检查。
2-您不必在“复杂”调试语句中选择性地包含该语句。所有语句都以这种方式包括在内。
3-调用log.debug会在记录之前执行以下操作:
if(repository.isDisabled(Level.DEBUG_INT))
return;
这基本上与调用日志相同。或猫。isDebugEnabled()。
然而!这就是log4j开发人员的想法(就像在他们的javadoc中一样,您可能应该遵循它。)
这是方法
public
boolean isDebugEnabled() {
if(repository.isDisabled( Level.DEBUG_INT))
return false;
return Level.DEBUG.isGreaterOrEqual(this.getEffectiveLevel());
}
这是它的javadoc
/**
* Check whether this category is enabled for the <code>DEBUG</code>
* Level.
*
* <p> This function is intended to lessen the computational cost of
* disabled log debug statements.
*
* <p> For some <code>cat</code> Category object, when you write,
* <pre>
* cat.debug("This is entry number: " + i );
* </pre>
*
* <p>You incur the cost constructing the message, concatenatiion in
* this case, regardless of whether the message is logged or not.
*
* <p>If you are worried about speed, then you should write
* <pre>
* if(cat.isDebugEnabled()) {
* cat.debug("This is entry number: " + i );
* }
* </pre>
*
* <p>This way you will not incur the cost of parameter
* construction if debugging is disabled for <code>cat</code>. On
* the other hand, if the <code>cat</code> is debug enabled, you
* will incur the cost of evaluating whether the category is debug
* enabled twice. Once in <code>isDebugEnabled</code> and once in
* the <code>debug</code>. This is an insignificant overhead
* since evaluating a category takes about 1%% of the time it
* takes to actually log.
*
* @return boolean - <code>true</code> if this category is debug
* enabled, <code>false</code> otherwise.
* */