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)
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...