Search code examples
sonarqube

[Sonarqube][Java]Printf-style format strings should be used correctly


it seems that a new rule is available with latest version. I have several issue reported as "Printf-style format strings should be used correctly (squid:S3457)"

I don't understand the description and what is wrong in my case:

LOGGER.info("Checking for client process pid: {0}", parentProcessId);
// issue: String contains no format specifiers

In the rules description we have:

java.util.Logger logger;
logger.log(java.util.logging.Level.SEVERE, "Result {0}.", myObject.toString()); // Noncompliant; no need to call toString() on objects
logger.log(java.util.logging.Level.SEVERE, "Result.", new Exception()); // compliant, parameter is an exception
logger.log(java.util.logging.Level.SEVERE, "Result '{0}'", 14); // Noncompliant {{String contains no format specifiers.}}

and

java.util.Logger logger;
logger.log(java.util.logging.Level.SEVERE, "Result {0}.", myObject);
logger.log(java.util.logging.Level.SEVERE, "Result {0}'", 14);

What's the difference with my cases? Can you help me understand what is the correct way to write it?


Solution

  • understood my error, with slf4j logger, {} needs to be used instead of {0}