我正在使用最新的 Eclipse 和 Sonar 插件
在answer对于日志记录,有以下行:
log.debug("Request body: {}", new String(body, "UTF-8"));
只有在 DEBUG 级别才应该创建字符串:
/**
* 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
:
"Preconditions" and logging arguments should not require evaluation (squid:S2629)
并给出连接的例子
logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages
这是误报 Sonar 警告还是我遗漏了什么?
这不是 this question 的副本这通常是在询问规则概念,它是串联的,但不是通过将对象创建为 new String
还有 link的答案说创建 new Date()
不会造成内置格式的问题:
public static void main(String[] args) { LOGGER.info("The program started at {}", new Date()); } }
Logging this way, you avoid performance overhead for strings concatenation when nothing should be actually logged.
最佳答案
非DEBUG模式行
log.debug("Request body: {}", new String(body, "UTF-8"));
代替
log.debug(MessageFormatter.format("Request body: {}", new String(body, "UTF-8")));
避免创建通过 MessageFormatter.format(String messagePattern, Object arg)
创建的字符串,但不是由 new String(body, "UTF-8")
创建的另一个字符串的创建。
这意味着它不是误报,因为参数是在调用日志记录方法之前先计算的。
只要SLF4J does not support lambda expression to lazy evaluate arguments , 以下实用方法可用作解决方法:
private static Object lazyToString(final Supplier<String> stringSupplier) {
return new Object() {
@Override
public String toString() {
return stringSupplier.get();
}
};
}
这可以用于将字节数组转换为字符串仅限于 DEBUG 模式:
log.debug("Request body: {}", lazyToString(() -> new String(body, StandardCharsets.UTF_8)));
关于java - Eclipse - Sonar S2629 可能误报新字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031847/