2015-09-10 106 views
1

當前logback.xml如何在java logback中打印自定義堆棧跟蹤?

<appender name="FILEOUT3" class="ch.qos.logback.core.FileAppender"> 
    <file>D:/${byDay}.log</file> 
    <append>true</append> 
    <encoder> 
     <Pattern>%d{HH:mm:ss} %-5level %msg%replace(%xException){"\n", ">> "}%nopex%n</Pattern> 
    </encoder> 
</appender> 

當前的結果:

 
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] 

我想結果是:

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

重要!
同時打印

我想知道的。
如何更改logback.xml?

回答

2

您不能通過僅更改logback.xml來實現此目的,因爲由%replace調用的ReplacingCompositeConverter僅使用靜態字符串作爲替換(無日期或PatternLayout的任何其他轉換字)。

爲了達到你的目標,你應該創建自定義轉換器(如果你用我的注意,它應該被放置在ch.qos.logback.core.pattern包)

package ch.qos.logback.core.pattern; 

import ch.qos.logback.classic.PatternLayout; 

import ch.qos.logback.core.pattern.parser.Node; 
import ch.qos.logback.core.pattern.parser.Parser; 
import ch.qos.logback.core.spi.ScanException; 

public class ReplacingAndParsingCompositeConverter<E> extends ReplacingCompositeConverter<E> { 

    @Override 
    protected String transform(E event, String in) { 
     if (!started) { 
      return in; 
     } 

     String parsedReplacement; 

     try { 
      Parser<E> p = new Parser<E>(replacement); 
      p.setContext(getContext()); 
      Node t = p.parse(); 
      Converter<E> c = p.compile(t, PatternLayout.defaultConverterMap); 
      ConverterUtil.setContextForConverters(getContext(), c); 
      ConverterUtil.startConverters(c); 
      StringBuilder buf = new StringBuilder(); 
      while (c != null) { 
       c.write(buf, event); 
       c = c.getNext(); 
      } 
      parsedReplacement = buf.toString(); 
     } catch (ScanException e) { 
      e.printStackTrace(); 
      parsedReplacement = replacement; 
     } 
     return pattern.matcher(in).replaceAll(parsedReplacement); 
    } 
} 

那麼你應該申報此轉換器在logback.xml中使用< conversionRule/>並使用它替代舊的%替換。

<configuration ...> 
    <conversionRule conversionWord="replaceAndParse" converterClass="ch.qos.logback.core.pattern.ReplacingAndParsingCompositeConverter" /> 

    <appender name="FILEOUT3" class="ch.qos.logback.core.FileAppender"> 
     <file>D:/${byDay}.log</file> 
     <append>true</append> 
     <encoder> 
      <Pattern>[%d{HH:mm:ss.SSS}] %-5level %msg%replaceAndParse(%xException){"(\r?\n)", "$1[%d{HH:mm:ss.SSS}]"}%nopex%n</Pattern> 
     </encoder> 
    </appender> 
    .... 
</configuration> 
+0

謝謝!你能把簡單的代碼發給我嗎? [email protected] /我很抱歉。我的英語不太好。 –

+0

更換/模式錯誤:(如何修復源代碼? –

+0

您能澄清您的評論嗎?我不確定我是否關注您 –