I try to log an Exception with SLF4J and Logback like this:
try {
//code that could throw exception
} catch (DataAccessException e) {
log.error("Problem with id={}", id, e);
}
In my SonarQube analysis I get the violation: "Either log or rethrow this exception".
I'm using SLF4J 1.7.36 and Logback 1.2.11. so the corresponding SLF4J interface method has the signature:
public void error(String format, Object arg1, Object arg2);
and the implementing Logback Logger
is:
public void error(String format, Object arg1, Object arg2) {
filterAndLog_2(FQCN, null, Level.Error, format, arg1, arg2, null);
}
public void filterAndLog_2(final String localFQCN, final Marker marker, final Level level, final String msg, final Object param1, final Object param2, final Throwable t) {
[...]
}
The last parameter is treated as an Object
and not as Throwable
. Other posts claim that since SLF4J 1.6 the last parameter is always treated as Exception, e.g. https://stackoverflow.com/a/6374166/15110545 but that doesn't seems to be true. How can I solve the Sonar violation?