详见:https://www.cnblogs.com/leiwei/p/14674143.html
相同点
下面代码的结果相同:
logger.debug("hello, world");
等同下面代码:
if(logger.isDebugEnabled()){
logger.debug("hello, world");
}
也就是说,无论是否判断,日志级别大于 debug,如: info,这两段代码,都不会输出日志
区别
不需要判断情况
如果像下面代码,则不需要判断:
logger.debug("hello, world");
调用 logger.debug() 方法时,只是简单打印,不涉及到频繁拼装字符串、大量的运算
需要判断情况
如果像下面代码,则需要:
if(logger.isDebugEnabled()){
logger.debug("hello" + "world"+"哈哈"+"呵呵"); // 频繁拼接字符串
logger.debug(String.format("hello, %s,%s", "world","哈哈")) // 格式化字符串
logger.debug("order price: {}", calcPrice()); // 调用方法
}
如果是 info 日志级别,不会输出日志,但调用 logger.debug() 方法时,会执行拼接字符串、String.format()、calcPrice() 运算,造成了额外的性能损耗,所以为了避免额外消耗,要加判断