我们公司刚刚开办了一个声纳农场.我很好奇我们的代码质量,并希望改进.
我有一个包含此类记录器调用的代码:
LOGGER.error(String.format("Cannot load object in status %s (%s)", status, statusDescription), e);
LOGGER.info(String.format("%s object(s) loaded in status %s (%s)", objects.size(), status, statusDescription));
Run Code Online (Sandbox Code Playgroud)
声纳触发规则squid:S262,Invoke方法只有条件地"Preconditions"和记录参数不应该要求评估.由两条线引发的规则.
关于这个,我真的不太明白发生了什么.解释接缝不适合我的用例.Sonar doc提供了这个例子:
logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages
Run Code Online (Sandbox Code Playgroud)
我完全理解(调试不会记录在生产中,因此会发生不必要的操作).但是对于信息和错误级别,我会假设你想要记录它.而且,就我而言,我希望两者都被记录下来.
哪种方法好?不同的重写不使用String.format?调整声纳不在信息/错误级别上触发?只是忽略这个声纳?别的什么?