Error-prone: Check slf4j log messages are constant

Created on 9 Mar 2017  Â·  6Comments  Â·  Source: google/error-prone

slf4j expects format strings to prevent unnecessary string concatenation; as a result, these format strings should be constant.

Positive example:

void f(String param) {
    ...
    // BUG: Diagnostic contains: non-constant expression
    log.warn("some message param=" + param);
}

Negative example:

void f(String param) {
    ...
    log.warn("some message param {}", param);
}

with extension to the full set of slf4j Logger methods.

I'd be happy to contribute this if appropriate/will set up as a plugin otherwise.

Type-NewCheck

Most helpful comment

Another thing that could be checked is that the number of parameters matches the number of {} expressions in the format string.

All 6 comments

another negative example:

void f(String param) {
    ...
    log.warn("some message param {}" + "  for f method", param);
}

Another thing that could be checked is that the number of parameters matches the number of {} expressions in the format string.

If you are willing to use findbugs, all those rules already exist.

https://github.com/KengoTODA/findbugs-slf4j

Correct. We're actually trying to move away from Findbugs.

On Fri, Mar 17, 2017, 19:12 Matt Nelson notifications@github.com wrote:

If are willing to use findbugs, all those rules already exist.

https://github.com/KengoTODA/findbugs-slf4j

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/google/error-prone/issues/565#issuecomment-287446154,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGOdwQbabwaB_jNUaXPTpkyKCLxpIclYks5rmtr4gaJpZM4MXoLd
.

Hello, I also developed a Errorprone plugin for SLF4J users, you may check it:
https://github.com/KengoTODA/errorprone-slf4j

Was this page helpful?
0 / 5 - 0 ratings