Substituting parameters in log message and add a Throwable in Log4j 2

There is an undocumented feature in Log4j 2 (and SLF4J 1.6+).
One can pass Throwable as the very last parameter. And it would then get a special treatment.
See AbstractLogger#logMessage(String, Level, Marker, String, Object...) and ReusableParameterizedMessage#initThrowable().

There is no need to bother with ParametrizedMessage. The code in the question will work just as expected:

logger.error("Logging in user {} with birthday {}", user.getName(), user.getBirthdayCalendar(), exception);

Leave a Comment