Should we use isDebugEnabled() while logging calculated data with Logback?

后端 未结 2 856
孤街浪徒
孤街浪徒 2021-01-28 01:03

Although in some tutorials, for example here (Parametrized logging section), said that Logback message {} parametrization help us to avoid unnecess

2条回答
  •  旧时难觅i
    2021-01-28 01:41

    Take a look at the example here

    Since 2.4, methods have been added to the Logger interface to support lambda expressions. The new methods allow client code to lazily log messages without explicitly checking if the requested log level is enabled. For example, previously one would write:

    // pre-Java 8 style optimization: explicitly check the log level
    // to make sure the expensiveOperation() method is only called if necessary
     if (logger.isTraceEnabled()) {
         logger.trace("Some long-running operation returned {}", expensiveOperation());
     }
    

    With Java 8, the same effect can be achieved with a lambda expression:

    // Java-8 style optimization: no need to explicitly check the log level:
    // the lambda expression is not evaluated if the TRACE level is not enabled
    logger.trace("Some long-running operation returned {}", () -> expensiveOperation());
    

提交回复
热议问题