log.debug是否需要判断级别

在Java开发中,对于logger.debug的日志输出,有人认为应该先判断日志级别以提高性能。然而,日志框架内部已包含判断机制,额外的if检查并不必要。官方建议表明,这种外部判断带来的性能提升微乎其微,而且在启用debug时可能导致两次判断。因此,直接使用logger.debug方法更简洁高效。所谓的性能影响通常是过度担忧。
摘要由CSDN通过智能技术生成

在实际开发中不少人在输出debug日志的时候会先判断一下日志框架是否允许debug如下

if(logger.isDebugEnabled()) { 
  logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}

会觉得性能会更好一些,那么这么做是否有必要呢?其实logger.debug方法内部本身就会进行一次判断,外面的if判断就显得多余,针对这个问题日志官网是结论是什么呢?

Chapter 2: Architecture

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值