避免串联连接大型日志消息

时间:2014-06-11 09:25:34

标签: java log4j slf4j

使用类似slf4j的一个优点是它通过使用参数化日志记录来避免字符串连接。但是,当你有很长时间时,你如何避免性能损失 记录消息?

logger.debug("This is a very long message that prints two values." +
    " However since the message is long, we still incur the performance hit" +
    " of String concatenation when logging {} and {} ", value1, value2);

有没有办法避免这种性能成本,而不使用丑陋的if块来检查日志级别?

 if (logger.isDebugEnabled()) {
     logger.debug("This is a very long message that prints two values." +
         " However since the message is long, we still incur the performance hit" +
         " of String concatenation when logging {} and {}", value1, value2);
 }

1 个答案:

答案 0 :(得分:4)

没有字符串连接。

编译器将为您创建一个长字符串文字。