Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback: controlling the formatting of exception stacktraces

I'm using Logback 1.0.13 on a Scala/Play 2.2.0 app. Existing config looks like:

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>${application.home}/logs/application.log</file>
    <encoder>
     <pattern>%date [%level][%logger{1}][%thread{1}] %message%xException%n</pattern>
    </encoder>
  </appender>

I'm looking if there's a way to configure it so exception traceback lines have a customized separator. Instead of

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:92) ~[play_2.10-2.2.0.jar:2.2.0]
at play.api.Configuration.reportError(Configuration.scala:570) ~[play_2.10-2.2.0.jar:2.2.0]
at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[play-jdbc_2.10-2.2.0.jar:2.2.0]

I'd like to put some chars in front of each line like this:

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
>>> at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:92) ~[play_2.10-2.2.0.jar:2.2.0]
>>> at play.api.Configuration.reportError(Configuration.scala:570) ~[play_2.10-2.2.0.jar:2.2.0]
>>> at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[play-jdbc_2.10-2.2.0.jar:2.2.0]
like image 511
seand Avatar asked Apr 14 '14 18:04

seand


People also ask

What is Logback configuration?

Logback is one of the most widely used logging frameworks in the Java Community. It's a replacement for its predecessor, Log4j. Logback offers a faster implementation, provides more options for configuration, and more flexibility in archiving old log files.

What is Ch QOS Logback?

GitHub - qos-ch/logback: The reliable, generic, fast and flexible logging framework for Java.

Where should the Logback xml be?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

How do I print the thread ID in Logback?

so just write %t on your configuration file in logback. xml reference document : Link and find Outputs the ID of the thread that generated the logging event. [%t] prints thread name not thread id with logback.


1 Answers

I figured out something like this works:

       <pattern>%date [%level][%logger{1}][%thread{1}] 
         %message%replace(%xException){"\n", "\\n"}%nopex%n</pattern>  

The %replace mechanism works on a stacktrace text. You also need %nopex to prevent the raw stacktrace from showing up again; otherwise Logback "helpfully" notices you omitted the trace and includes it for you.

like image 107
seand Avatar answered Oct 03 '22 18:10

seand