How to log exception and message with placeholders with SLF4J

pjp picture pjp · May 10, 2011 · Viewed 43k times · Source

What's the correct approach to log both an error message and an exception using SLF4J?

I've tried doing this but the exception stack trace is never printed:

logger.error("Unable to parse data {}", inputMessage, e);

In this case I want to populate {} with the inputMessage as well as logging out the exception stacktrace.

The only way I can see to do this would be to do this:

logger.error("Unable to parse data " + inputMessage, e);

which is not pretty.

Answer

Ceki picture Ceki · May 10, 2011

As of SLF4J version 1.6, SLF4J will interpret the last parameter as you intended, i.e. as an exception. You must be using an older version of SLF4J API.