SonarQube为具有消息和Throwable的日志方法添加了“参数不足”的警告。

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

Why does SonarQube add "Not enough arguments." for log methods with message and Throwable?

问题

我正在使用 self4j 进行日志记录:

LOGGER.info("在保存数据时发生异常 {}", be);

但上述语句显示了 Sonar 问题 参数不足。在 Eclipse 中使用 Sonar Lint 5.3.1。

如果我将上面的 Logger 更新如下,则不会显示任何问题:

LOGGER.info("在保存数据时发生异常", be);

或者,如果我将日志记录字符串提取到任何常量文件中,那么也能正常工作:

private static final String LOG_STR = "在保存数据时发生异常 {}";

LOGGER.info(LOG_STR, be);

为什么在不同的情况下它的行为不同呢?

英文:

I am using self4j for putting logger

LOGGER.info("Exception occurred while saving data {}", be);

But above statement showing sonar issue Not enough arguments. Using sonar lint 5.3.1 in eclipse

If I update above Logger as below then its not showing any issue

LOGGER.info("Exception occurred while saving data", be);

Or if I extract logger string in any constant file then its also working fine

private static final String LOG_STR = "Exception occurred while saving data {}";

LOGGER.info(LOG_STR , be);

Why its behaving different in different scenario?

答案1

得分: 2

这篇帖子中有两个问题。

为什么带有{}的消息会显示Not enough arguments.

SLF4J Logger 类为每个日志方法提供了几种变体:

  1. info(java.lang.String,java.lang.Object)
  2. info(java.lang.String,java.lang.Object...)
  3. info(java.lang.String,java.lang.Throwable)

第一种和第二种变体接受一个消息和要注入其中的参数。第三种接受静态消息,因为异常的处理方式不同。我相信 be 是一个异常。这意味着在这里:

// 问题:参数不足
LOGGER.info("保存数据时发生异常 {}", be);

你使用了不支持{}的第三个选项。如果你只想显示异常消息并隐藏堆栈跟踪(在使用第三种变体时默认会记录堆栈跟踪),你应该在 be 上调用 toString()

// 好的,调用info(String, String),这样一个参数就可用了
LOGGER.info("保存数据时发生异常 {}", be.toString());

为什么带有常量的代码不会产生错误?

我相信规则中存在一个错误,你应该在 SonarSource 社区 上报告它。

英文:

There are two questions in this post.

Why message with {} has Not enough arguments.?

SLF4J Logger class has a few variants for every log method:

  1. info(java.lang.String,java.lang.Object)
  2. info(java.lang.String,java.lang.Object...)
  3. info(java.lang.String,java.lang.Throwable)

The first and the second variants take a message and parameters which will be injected into it. The third takes a static message because exceptions are handled in a different way. I believe be is an exception. It means that here:

// issue: not enough arguments
LOGGER.info("Exception occurred while saving data {}", be);

You execute the third option which doesn't support {}. If you want to display only exception message and hide the stack trace (which will be logged by default when you use the third variant) - you should call toString() on be.

// okay, call info(String, String), so one parameter is available
LOGGER.info("Exception occurred while saving data {}", be.toString());

Why the code with constant doesn't produce an error?

I believe there is a bug in the rule, which you should report it on SonarSource Community.

huangapple
  • 本文由 发表于 2020年8月21日 16:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63519197.html
匿名

发表评论

匿名网友

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

确定