Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sonarqube

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?

like image 288
Aurélien Pupier Avatar asked Jan 03 '23 11:01

Aurélien Pupier


1 Answers

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

like image 177
Aurélien Pupier Avatar answered Apr 18 '23 10:04

Aurélien Pupier