我在tomcat上使用Logback(7.0.37)。我想配置logback,它可以为我编写Web可访问的日志文件,但我甚至可以将其配置为在控制台上写入。我无法在控制台上格式化日志消息。它总是以默认格式和异常返回给我。
%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)
答案 0 :(得分:7)
Logback-access用于访问日志。 Logback-access与logback-classic具有不同的意图。因此,在logback-access配置文件中,您需要使用&#34; ch.qos.logback中的PatternLayoutEncoder
。访问&#34;包不是&#34; ch.qos.logback。经典。编码器&#34;封装
例如,写:
<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 :(得分:2)
我在寻找异常解决方案时找到了这个主题
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的默认访问日志。