Recorder format and resettable, slf4j

In the process of converting some old logs with String.format to a new variant of slf4j {}, I came across this case:

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

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

error(String msg, Throwable t)

So, I need to save String.format in this case ?!

Why not:

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

+5
source share
1 answer

As with SLF4J 1.6.0, if there are several parameters, and if the last argument in the log statement is an exception, then SLF4J will assume that the user wants the last argument to be considered as an exception, and not just a parameter.

So, the record (in version 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 ...

+8
source

Source: https://habr.com/ru/post/1269752/


All Articles