Quick and dirty way: logger.warn ("your_message_here \ n {}", err.toString ());
Itβs best to write your own custom conversion specifier (see the protocol manual for custom error handling to abstract from your application code, and you can customize it using the custom conversion word in your logback.xml file.
Here is a simple example that will do what you need:
package com.example.logging; import ch.qos.logback.classic.pattern.ClassicConverter; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.IThrowableProxy; import ch.qos.logback.classic.spi.ThrowableProxy; public class MyExceptionConverter extends ClassicConverter { @Override public String convert(ILoggingEvent event) { IThrowableProxy itp = event.getThrowableProxy(); if (itp instanceof ThrowableProxy) { ThrowableProxy tp = (ThrowableProxy)itp; return tp.getThrowable().toString(); } return ""; } }
Then in your logback.xml file you will need to reference it like this:
<conversionRule conversionWord="myCon" converterClass="com.example.logging.MyExceptionConverter" />
And then use% myCon in the encoder pattern, for example:
<encoder> <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} %logger{0}: %myCon %nopex%n</pattern> </encoder>
Note that adding% nopex stops normal exception handling
source share