在实际开发中不少人在输出debug日志的时候会先判断一下日志框架是否允许debug如下
if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
会觉得性能会更好一些,那么这么做是否有必要呢?其实logger.debug方法内部本身就会进行一次判断,外面的if判断就显得多余,针对这个问题日志官网是结论是什么呢?
One possible way to avoid the cost of parameter construction is by surrounding the log statement with a test. Here is an example.
if(logger.isDebugEnabled()) { logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i])); }
This way you will not incur the cost of parameter construction if debugging is disabled for logger
. On the other hand, if the logger is enabled for the DEBUG level, you will incur the cost of evalu