是否需要进行 if(log.isDebugEnabled()) { ... } 检查? [英] Is there a need to do a if(log.isDebugEnabled()) { ... } check?
问题描述
是否需要做一个显式的 if(log.isDebugEnabled()) { ... } 检查?
is there a need to do an explicit if(log.isDebugEnabled()) { ... } check?
我的意思是我看到一些帖子提到 log.debug("something") 在执行日志记录之前会进行隐式调用以查看是否已启用调试模式日志记录.我是否遗漏了某些东西,或者在使用它之前是否需要执行一个中间步骤?
I mean i have seen some post mentioning that log.debug("something") does an implicit call to see if debug mode logging has been enabled, before it does the logging. Am i missing something or is there an intermediary step that is to be performed before this is used?
谢谢!
log.debug("ResultSet rs is retrieved from OracleTypes");
对比
if(log.isDebugEnabled()){
log.debug("ResultSet rs is retrieved from OracleTypes");
}
写了一篇关于这个:http://java.sg/whether-to-do-a-isdebugenabled-checking-before-printing-out-your-log-statement/
推荐答案
声明:
if(log.isDebugEnabled()){
仅用于性能原因.它的使用是可选的,因为它在内部被 log 方法调用.
Is used just for performance reasons. It's use is optional since it is called by the log method internally.
但是现在你问这个检查是否是内部进行的,那我为什么要使用它?很简单:如果你记录一些像这样简单的东西:
But now you ask if this check is made internally, so why should I use it? It's very simple: if you log something as simple as this:
log.debug("ResultSet rs is retrieved from OracleTypes");
那么你不需要做任何检查.如果您使用附加运算符 (+) 编写要记录的字符串,如下所示:
Then you don't need to do any check. If you compose a string to log using the append operator (+) like this:
log.debug("[" + System.getTimeInMillis() + "] ResultSet rs is retrieved from OracleTypes");
在这种情况下,您应该检查是否启用了日志,因为如果没有,即使没有创建日志,字符串组合也是.而且我必须提醒您,使用运算符+"来连接字符串是非常低效的.
In this case you should check if the log is enabled or not, because if it isn't, even if the log is not made, the string composition is. And I must remind you that the use of operator "+" to concatenate strings is very inefficient.
这篇关于是否需要进行 if(log.isDebugEnabled()) { ... } 检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!