如何解决这个问题:“不应直接使用标准输出来记录任何内容”。

huangapple go评论83阅读模式
英文:

How to solve this issue "Standard outputs should not be used directly to log anything "

问题

我需要一个打印语句。但是 Sonar 不允许这种类型。

String txt = "Something";
System.out.println("Print: " + txt);

预期输出:

Print: Something

我尝试了这种格式 logger.log()。但对于我们的要求不起作用。

英文:

I need a print statement. But sonar not allowed this type.

String txt="Something";
System.out.println("Print: "+txt);

Expected output:

Print: Something

I tried this format logger.log().its not working for our requirement.

答案1

得分: 0

如果记录器不符合您的要求,请通过配置使其符合要求。

有一些广泛使用的日志记录框架具有高度可配置性,我怀疑它们将无法执行您使用普通的 System.out.println 进行的任何操作。

例如,可以查看非常流行的 SLF4J 和作为日志提供程序的 Logback

http://www.slf4j.org/

http://logback.qos.ch/

英文:

If logger is not meeting your requirements, make it to do so by configuring it.

There are some widely used logging frameworks that are highly configurable and I doubt that they will not be able to do whatever you are doing with plain System.out.println

For example, check out very popular SLF4J and Logback as logging provider.

http://www.slf4j.org/

http://logback.qos.ch/

答案2

得分: 0

Sonarqube 不喜欢在日志函数的参数中使用 System.out,也不喜欢如果你在传递给日志函数的参数中进行字符串连接或其他计算。在记录日志之前,请在单独的一行上构造完整的消息:

String txt = "Print: " + "Something";

String logMessage = "Print: " + txt;
logger.log(Level.INFO, logMessage);
英文:

Sonarqube does not like it if you use System.out, or if you have string concatenation or other computation in the arguments you pass to the logging function. Construct the full message on a separate line before logging it:

String txt = "Print: " + "Something";

String logMessage = "Print: " + txt;
logger.log(Level.INFO, logMessage);

huangapple
  • 本文由 发表于 2020年8月19日 18:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63485341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定