Print Stacktraces in a different file with Logback

14 Aug 2017

Sometimes you don’t want an ugly stacktrace in your beautiful logs. This is a method on “How to hide the mess elsewhere” with Logback.

Exception

First try (it’s not gonna work)

The first try is always the same: define two patterns one with nopex for ignoring exceptions and another one with the exception. Something like that:

<property name="noExceptionPattern" value="%nopex %d{dd/MM/yyyy-HH:mm:ss.SSS} [%thread] %-5level %logger{36}:%L - %msg%n" />
<property name="exceptionPattern" value="%nopex %d{dd/MM/yyyy-HH:mm:ss.SSS} [%thread] %-5level %logger{36}:%L - %xException{full}%n" />

But as soon as you try that configuration the awfull truth will strike you in the face: it does not work. exceptionPattern will print a line every time even if they are no Exception ! :sob:

But it’s quite easy to make it work !

Make it work !

To make it work we have to add something: a filter. To show how it works, I’m going to use a JaninoEventEvaluator. It takes an arbitrary Java language block returning a boolean value as the evaluation criteria:

<appender name="stackTracerFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>&LOG_DIR;/stacktraces.log</file>
  <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
    <evaluator>
      <expression>return (throwable == null);</expression>
    </evaluator>
    <OnMismatch>NEUTRAL</OnMismatch>
    <OnMatch>DENY</OnMatch>
  </filter>
  <encoder>
    <charset>UTF-8</charset>
    <pattern>${exceptionPattern}</pattern>
  </encoder>
</appender>

If the exception is null the appender will not print (or in this case write) a line.

To make it work you have to add Janino to your pom.

That’s it ! Easy !

Stay tuned and See ya !