【发布时间】:2019-04-01 13:53:48
【问题描述】:
我正在使用最新的 Eclipse 和 Sonar 插件
在answer 用于记录有以下行:
log.debug("Request body: {}", new String(body, "UTF-8"));
只有在调试级别才应该创建字符串:
/**
* Log a message at the DEBUG level according to the specified format
* and argument.
* <p/>
* <p>This form avoids superfluous object creation when the logger
* is disabled for the DEBUG level. </p>
*
* @param format the format string
* @param arg the argument
*/
public void debug(String format, Object arg);
但 Sonar 将其标记为 squid:S2629:
“先决条件”和日志记录参数不应需要评估 (squid:S2629)
并给出连接示例
logger.log(Level.DEBUG, "出了点问题:" + message); // 不合规的;即使日志级别太高而无法显示 DEBUG 消息,也会执行字符串连接
这是误报声纳警告还是我遗漏了什么?
这不是 this question 的重复,它通常询问规则概念,即连接,但不是将创建对象格式化为 new String
另外link 的答案说创建new Date() 不会产生内置格式的问题:
public static void main(String[] args) { LOGGER.info("The program started at {}", new Date()); } }以这种方式记录,可以避免在实际不记录任何内容时字符串连接的性能开销。
【问题讨论】:
标签: java eclipse sonarqube sonarlint-eclipse