[JAVA LOGGER] Rule about built-in formatting to construct logger message

705 views
Skip to first unread message

mickael.b...@gmail.com

unread,
Apr 11, 2018, 9:03:49 AM4/11/18
to SonarQube
Hi,

I have some JAVA method which use logger with the following format :

if (LOGGER.isInfoEnabled()) {
    LOGGER
.info("something" + aVariable );
}

The previous is ok about sonar rules, because it detect the "isInfoEnabled()".

But, in some method, i have multiple info logger. So, in the beginning of the method, i put the "isInfoEnabled()" in a boolean, and use it like this :

boolean isInfoEnabled = LOGGER.isInfoEnabled();

if (isInfoEnabled) {
    LOGGER
.info("something" + aVariable );
}

// SOME CODE

if (isInfoEnabled) {
    LOGGER
.info("something" + aVariable + "maybeSomethingElse");
}

And here, a major sonar rule trigger : "Use the built-in formattin to construct this argument".
Sonar rule seem to not understand than the boolean "isInfoEnabled" is the one expected as "LOGGER.isInfoEnabled".

Which could be the better solution ? improve sonar rule or i'm goign wrong with my boolean ?



Nicolas Peru

unread,
Apr 27, 2018, 10:30:23 AM4/27/18
to mickael.b...@gmail.com, SonarQube
Hi, 

The rule only check for common pattern with the call to isLevelEnabled method in the condition prior to the log call. Coming to think of it, one (arguable) flaw about your approach is that the level could be changed between calls to your method (let's be honest this is a thin and theoritical argument).

I don't see a lot of value (and where to stop and even how to) trying to follow any indirection that could be introduced.
As such I think that either you could mark those issues as won't fix or refactor to stick to the common pattern of logging (and rely on compiler and JVM optimization)

Cheers, 


--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/7d03eca4-e487-4ca2-9fdb-7ec89e7a8a34%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Nicolas Peru | SonarSource
Reply all
Reply to author
Forward
0 new messages