Logger format and throwable, slf4j, arguments

Christophe Roussy :

In the process of converting some old loggers from String.format to the newer slf4j {} variant, I stumbled upon this case:

logger.error(String.format("%s ... %s ... %s", ...), e);

I would like to use only {} and remove the String format, however, the logger method signature which includes the throwable is:

error(String msg, Throwable t)

So I do have to keep the String.format in this case ?!

Why is there no:

error(Throwable t, String format, Object... arguments)

Mathias G. :

As of SLF4J 1.6.0, in the presence of multiple parameters and if the last argument in a logging statement is an exception, then SLF4J will presume that the user wants the last argument to be treated as an exception and not a simple parameter.

So, writing (in SLF4J version 1.7.x and later)

logger.error("one two three: {} {} {}", "a", "b", 
          "c", new Exception("something went wrong"));

Will do what you want to achieve...

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=432947&siteId=1
Recommended