2013-03-22 110 views
2

我在Tomcat(7.0.37)上使用Logback。我想配置logback,在其中它可以爲我寫網絡可訪問的日誌文件,但我甚至可以配置它在控制檯上寫入。我無法在控制檯上格式化日誌消息。它總是以默認格式和異常返回給我。tomcat上的Logback配置

%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n

這裏是我的logback-access.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration debug="true"> 
    <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> 

    <appender name="RootFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
     <file>testing</file> 
     <append>true</append> 

     <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> 
      <fileNamePattern>testing.%i</fileNamePattern> 
      <minIndex>1</minIndex> 
      <maxIndex>12</maxIndex> 
     </rollingPolicy> 

     <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 
      <maxFileSize>1MB</maxFileSize> 
     </triggeringPolicy> 

     <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> 
      <layout class="ch.qos.logback.classic.html.HTMLLayout"> 
       <pattern>%d{yyyy-MM-dd HH:mm:ss} %m%n</pattern> 
      </layout> 
     </encoder> 
    </appender> 

    <appender name="RootConsoleAppender" class="ch.qos.logback.core.ConsoleAppender"> 
     <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 
      <pattern>%d{yyyy-MM-dd HH:mm:ss} %-6level- %logger{36}: %msg%n</pattern> 
     </encoder> 
    </appender> 

    <appender-ref ref="RootFileAppender"/> 
    <appender-ref ref="RootConsoleAppender"/> 
</configuration> 

有了這個配置,我得到每當有一次例外HTTP請求到我的服務器如下:

18:08:19,951 |-ERROR in ch.qos.logback.core.ConsoleAppender[RootConsoleAppender] - Appender [RootConsoleAppender] failed to append. java.lang.ClassCastException: ch.qos.logback.access.spi.AccessEvent cannot be cast to ch.qos.logback.classic.spi.ILoggingEvent 
    at java.lang.ClassCastException: ch.qos.logback.access.spi.AccessEvent cannot be cast to ch.qos.logback.classic.spi.ILoggingEvent 
    at at ch.qos.logback.classic.PatternLayout.doLayout(PatternLayout.java:60) 
    at at ch.qos.logback.core.encoder.LayoutWrappingEncoder.doEncode(LayoutWrappingEncoder.java:134) 
    at at ch.qos.logback.core.OutputStreamAppender.writeOut(OutputStreamAppender.java:188) 
    at at ch.qos.logback.core.OutputStreamAppender.subAppend(OutputStreamAppender.java:212) 
    at at ch.qos.logback.core.OutputStreamAppender.append(OutputStreamAppender.java:103) 
    at at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:88) 
    at at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:48) 
    at at ch.qos.logback.access.tomcat.LogbackValve.invoke(LogbackValve.java:182) 
    at at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) 
    at at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 
    at at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    at at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at at java.lang.Thread.run(Thread.java:722) 

回答

6

Logback-Access用於訪問日誌。 Logback-access與logback-classic有不同的意圖。因此,在logback-access配置文件中,您需要使用「ch.qos.logback。access」包中的PatternLayoutEncoder而不是「ch.qos.logback。classic .encoder」包。

例如,寫:

<appender name="RootConsoleAppender" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder class="ch.qos.logback.access.PatternLayoutEncoder"> 
    <pattern>%clientHost %l %user %date "%r" %statusCode %bytesSent</pattern> 
    </encoder> 
</appender> 

請同時參閱列出可用於logback-access PatternLayout轉換字。

+1

更好的是,不要設置'class'屬性。它適用於''。 – 2015-08-19 13:26:26

1

我發現這個話題在尋找一個例外

ch.qos.logback.classic.AsyncAppender - Appender failed to append. java.lang.ClassCastException: ch.qos.logback.access.spi.AccessEvent cannot be cast to ch.qos.logback.classic.spi.ILoggingEvent 

的解決方案,這似乎是長期已知的問題,這裏報告:https://github.com/qos-ch/logback/pull/185

長話短說

AsyncAppender不適用於AccessEvent這是默認訪問日誌爲tomcat