8

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]
seand
  • 5,168
  • 1
  • 24
  • 37
  • I don't think logback has anything of this sort natively. You could probably write a custom logback appender and try achieving the same. – Aritra Apr 14 '14 at 18:17

1 Answers1

15

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.

seand
  • 5,168
  • 1
  • 24
  • 37
  • Is there any way how to make this platform independent? (there is "\r\n" line separator on Windows) Logback has %n conversion word, but I have no idea how to use this in the replace method. – Vlasta Dolejs Feb 12 '18 at 12:49