使用 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 block 来检查日志级别?
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);
}
最佳答案
没有字符串连接。
编译器将为您创建一个长字符串文字。
关于java - 避免对大型日志消息进行字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24159245/