[logback-user] Doubt on exception as last parameter behavior

Ceki ceki at qos.ch
Thu Nov 14 19:02:07 CET 2019


Hi Fabio,

Thank you for the great question.

In the SLF4J 1.7 series, the intended behavior is to treat the last 
parameter as an exception if it is of type Throwable.

In addition, in 2.0 and going forward, if the exception is passed 
explicitly (via the fluent API), then the type of the last argument does 
not matter (it will seen as a value and not an exception).

HTH,

On 14.11.2019 14:17, Fabio Cechinel Veronez wrote:
> Hello all,
> 
> I have a question regarding the behavior of an exception as last parameter.
> 
> It is very well known that an exception placed as last parameter will
> have a special handling, having its stack trace printed available in
> the final output. Ex:
> 
> ```
> logger.warn("Printing a value {} with exception's stacktrace", 23, new
> Exception("some message"));
> ```
> 
> Outputs something like:
> 
> ```
> 11:35:01.696 [main] WARN  App - Printing a value 23 with exception's stacktrace
> java.lang.Exception: some message
>      at App.main(App.java:7) ~[classes/:na]
> ```
> 
> My question is: what if the message string contains an interpolation
> expression for that exception parameter? Example:
> 
> ```
> logger.warn("Printing a value {} with an inlined exception {}", 23,
> new Exception("some message"));
> ```
> 
> Personally I would expect that in this case the exception would be
> printed just like any other regular parameter, not having its
> stracktrace printed. Basically, I would expected an exception to have
> a special handling only when it is the "+1" parameter.
> 
> It seems to have been the case up to logback-classic:1.0.13
> (slf4j-api:1.7.5). Using this version the log statement above prints:
> 
> ```
> 11:35:01.699 [main] WARN  App - Printing a value 23 with an inlined
> exception java.lang.Exception: some message
> ```
> 
> While using logback-classic:1.2.3 (slf4j-api:1.7.25) it prints:
> 
> ```
> 11:38:02.150 [main] WARN App - Printing a value 23 with an inlined exception {}
> java.lang.Exception: some message
>      at App.main(App.java:8)
> ```
> 
> Apparently the behaviour changed from logback:1.1.0 onwards.
> 
> Is this a new expected behavior?
> 
> Unfortunately I couldn't find in the documentation where the expected
> behavior of this particular situation is explained in more details. So
> I thought it to be a bit ambiguous. If there one, please, let me know.
> 
> Thanks in advance.
> 

-- 
--
Ceki Gülcü


More information about the logback-user mailing list