java - 避免对大型日志消息进行字符串连接

标签 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 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/

相关文章:

java - 由于 Maven 的 SLF4J 模块加载错误,Hibernate 无法在 Wildfly 上序列化对象

intellij-idea - 使用 Lombok 的 @Slf4j 和 Intellij 进行构建 : Cannot find symbol log

java - Log4j 验证异常是否作为最后一个参数传递

java - HelloWorld 程序中出现 NoClassDefFoundError

java - Jetty_run错误java.lang.TypeNotPresentException : Type javax.annotation.meta.When不存在

java - 线程完成时 Log4j 追加到日志文件

logging - 如何在 WSO2 ESB 中的每个服务日志中选择主机名

java - 如何设置 ListPreference 的默认值

java - 使用摘要异步签署 pdf

java - Logback - 获取方法名称