0

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.