[logback-user] ClassCastException with DBAppender in Logback 1.0.7
Moebius.Jonathan at swm.de
Moebius.Jonathan at swm.de
Thu Nov 8 12:30:38 CET 2012
Hello List,
I'm using the DBAppender with Logback 1.0.7 and I'm getting the following Exception:
11:54:18,066 |-ERROR in ch.qos.logback.access.db.DBAppender[DB] - problem appending event java.lang.ClassCastException: ch.qos.logback.classic.spi.LoggingEvent cannot be cast to ch.qos.logback.access.spi.IAccessEvent
at java.lang.ClassCastException: ch.qos.logback.classic.spi.LoggingEvent cannot be cast to ch.qos.logback.access.spi.IAccessEvent
at at ch.qos.logback.access.db.DBAppender.subAppend(DBAppender.java:36)
at at ch.qos.logback.core.db.DBAppenderBase.append(DBAppenderBase.java:108)
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.classic.Logger.appendLoopOnAppenders(Logger.java:280)
at at ch.qos.logback.classic.Logger.callAppenders(Logger.java:267)
at at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:449)
at at ch.qos.logback.classic.Logger.filterAndLog_1(Logger.java:421)
at at ch.qos.logback.classic.Logger.info(Logger.java:611)
at at de.swm.pmf.logtest.LoggingThread.run(LoggingThread.java:16)
at at java.lang.Thread.run(Unknown Source)"
I'm not quite certain what the problem is, but I was looking through the code and have found the following:
Logger.appendLoopOnAppenders has ILoggingEvent as an Parameter, this Parameter is passed through to the AppenderAttachableImpl.appendLoopOnAppenders, which is ok as the AAI is typed as AAI<ILoggingEvent>. The AAI has a List<Appender<E>> where E seems to be bound to the Type of the AAI, which is ILoggingEvent. UnsynchronizedAppenderBase is still typed as ILoggingEvent and therefore UnsynchronizedAppenderBase.doAppend takes an ILoggingEvent as Parameter and passes that through to ch.qos.logback.core.db.DBAppender.append, where we are leaving package ch.qos.logback.core.db for ch.qos.logback.access.db and the DBAppender there (a child of the core-DBAppender) is typed as DBAppender<IAccessEvent>, hence the ClassCastException.
What I'm not getting is, why the code compiled originally, because clearly the DBAppender cannot be typed ILoggingEvent and IAccessEvent at the same time.
I appreciate any help or pointers because searching the web gives nothing useful and I'm really stuck here.
Kind regards,
Jonathan
My configuration is pretty much straight forward, stolen from the example:
<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<appender name="DB" class="ch.qos.logback.access.db.DBAppender">
<connectionSource
class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://localhost:3306/logbackdb</url>
<user>root</user>
<password>mysqlroot</password>
</connectionSource>
<insertHeaders>false</insertHeaders>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
<logger name="logexample" additivity="false">
<appender-ref ref="DB" />
</logger>
</configuration>
More information about the Logback-user
mailing list