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

前端 未结 5 705
感情败类
感情败类 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 06:03

    It is about string concatenation performance. It's potentially significant if your have dense logging statements.

    (Prior to SLF4J 1.7) But only two parameters are possible

    Because the vast majority of logging statements have 2 or fewer parameters, so SLF4J API up to version 1.6 covers (only) the majority of use cases. The API designers have provided overloaded methods with varargs parameters since API version 1.7.

    For those cases where you need more than 2 and you're stuck with pre-1.7 SLF4J, then just use either string concatenation or new Object[] { param1, param2, param3, ... }. There should be few enough of them that the performance is not as important.

提交回复
热议问题