String concatenation performance in Log4j

后端 未结 2 1822
一个人的身影
一个人的身影 2021-01-18 22:54

I have often heard people saying that its one of the best practices to avoid String Concatenation and use {} instead while logging.

I was

2条回答
  •  死守一世寂寞
    2021-01-18 23:23

    The benefit of format strings in logging systems is, that the logging system can decide if the string concatenation must happen or not.

    Let's use these lines as example:

    log.debug("Count: " + list.size());
    log.debug("Count: {}", list.size());
    

    As long as the level for this logger is debug or below, there is no difference in performance, but if the log level is higher than debug the second line won't perform the concatenation at all.

提交回复
热议问题