tuk
tuk

Reputation: 6862

Log4j Validating if Exception is passed as last argument

I am using slf4j with log4j. My POM dependency looks like below -

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

As mentioned in SLF4J FAQ

The SLF4J API supports parametrization in the presence of an exception, assuming the exception is the last parameter.

In a is there a way to identify if exception is passed as a last argument to all logger's method call (like error, info)?

Upvotes: 1

Views: 452

Answers (1)

Didier L
Didier L

Reputation: 20579

There is no simple way to do this kind of check at compile time, or even at runtime.

You can however use a combination of FindBugs and the findbugs-slf4j plugin to check this statically with SLF4J_PLACE_HOLDER_MISMATCH and SLF4J_FORMAT_SHOULD_BE_CONST.

Upvotes: 3

Related Questions