Logger slf4j advantages of formatting with {} instead of string concatenation

前端 未结 5 691
感情败类
感情败类 2020-11-28 05:21

Is there any advantage of using {} instead of string concatenation?

An example from slf4j

logger.debug(\"Temperature set to {}. Old temp         


        
5条回答
  •  爱一瞬间的悲伤
    2020-11-28 05:49

    I think from the author's point of view, the main reason is to reduce the overhead for string concatenation.I just read the logger's documentation, you could find following words:

    /**
    * 

    This form avoids superfluous string concatenation when the logger * is disabled for the DEBUG level. However, this variant incurs the hidden * (and relatively small) cost of creating an Object[] before invoking the method, * even if this logger is disabled for DEBUG. The variants taking * {@link #debug(String, Object) one} and {@link #debug(String, Object, Object) two} * arguments exist solely in order to avoid this hidden cost.

    */ * * @param format the format string * @param arguments a list of 3 or more arguments */ public void debug(String format, Object... arguments);

提交回复
热议问题