logger.isDebugEnabled()的作用

本文解释了为什么在输出debug日志前使用logger.isDebugEnabled()判断可以提高系统效率。通过避免不必要的参数计算,尤其是当这些计算成本较高时,能显著减少资源消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在项目中经常会看到这样的代码:

if (logger.isDebugEnabled()) {
    logger.debug(message);
}
为什么要这样做呢?

  且看isDebugEnabled()的源码:

public boolean isDebugEnabled() {    
  if(repository.isDisabled( Level.DEBUG_INT))
      return false;
  return Level.DEBUG.isGreaterOrEqual(this.getEffectiveLevel());
}

  以下是debug()的源码:

public void debug(Object message) {
    if(repository.isDisabled(Level.DEBUG_INT))
        return;
    if(Level.DEBUG.isGreaterOrEqual(this.getEffectiveLevel())) {
        forcedLog(FQCN, Level.DEBUG, message, null);
    }
}

  可见,debug()中做了跟isDebugEnabled()几乎一样的判断,看起来直接调用debug()比先判断isDebugEnabled()更加效率。
  此时来看下面的代码:
logger.debug("The money is " + getTotalMoney());

  假设我们的日志级别设置为info,debug()方法调用后会判断if(repository.isDisabled(Level.DEBUG_INT)),然后return。但是在调用debug()方法时,必须提供参数。要获得参数,就需要执行getTotalMoney()并拼接。假设这个获取参数的过程需要10秒钟,则系统会在花费10秒后决定return,这显然很得不偿失。
  加上logger.isDebugEnabled()判断,只会使写日志的时间增加大概万分之一,但是如果不加此判断,系统可能需要花费更多的时间,所以在大多数情况下,在输出debug日志前加上logger.isDebugEnabled()比较好。
  
  参考文献:http://zhukewen-java.iteye.com/blog/1174017
  本人研究较浅,思维也不够严谨,文章主要用于自己存档和供新手们了解。若您发现有什么问题,欢迎评论指正,谢谢!

使用的是springboot , 数据库连接使用jdbc进行连接,现在有一个sql拦截器,代码如下 Intercepts(value = { @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}) @Slf4j public class SqlStatementInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object returnValue; long start = System.currentTimeMillis(); returnValue = invocation.proceed(); long end = System.currentTimeMillis(); long time = end - start; try { final Object[] args = invocation.getArgs(); //获取原始的ms MappedStatement ms = (MappedStatement)args[0]; Logger logger = LoggerFactory.getLogger(ms.getId()); Object parameter = null; //获取参数,if语句成立,表示sql语句有参数,参数格式是map形式 if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } // BoundSql就是封装myBatis最终产生的sql类 BoundSql boundSql = ms.getBoundSql(parameter); // 获取节点的配置 Configuration configuration = ms.getConfiguration(); //getSql(configuration, boundSql, sqlId, time); // 获取到最终的sql语句 String sql = showSql(configuration, boundSql); String namespace = ms.getId(); List<String> methodNames = Optional.ofNullable(LoaderAnnotation.getAnnoMap().get(NoFilter.class)).orElse(new ArrayList<>()); // 使用执行器的日志打印 if (methodNames.contains(NoFilterAspect.methodName.get()) || methodNames.contains(namespace)) { if (logger.isDebugEnabled()) { logger.debug("耗时【{}ms】,SQL: {}", time, sql); } if (logger.isDebugEnabled()) { logger.debug("result->{}", JacksonUtil.obj2json(returnValue)); } } else { logger.info("耗时【{}ms】,SQL: {}", time, sql); logger.info("result->{}", JacksonUtil.obj2json(returnValue)); } } catch (Exception e) { log.error("拦截sql处理出差{}", e.getMessage()); } return returnValue; } 我希望在上述代码中加入实时监控数据库连接池信息,如何实现
最新发布
07-11
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值