1

使用logback-1.0.13.jar和 JDK 1.6u34。

我有一个 Java Web 应用程序 (WAR),其中包含以下内容WEB-INF/classes/logback.xml

<configuration debug="true" scan="true" scanPeriod="5 minutes">
    <appender name="logManager-dbAppender" class="ch.qos.logback.classic.db.DBAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>NEUTRAL</onMismatch>
        </filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>NEUTRAL</onMismatch>
        </filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource">
            <jndiLocation>java:comp/env/jdbc/dbLogging-local</jndiLocation>
        </connectionSource>
    </appender>

    <root level="ALL">
        <appender-ref ref="logManager-dbAppender" />
    </root>
</configuration>

以及以下(所有网络应用程序的${TOMCAT_HOME}/conf/context.xml全局):context.xml

<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <Resource
        name="jdbc/dbLogging-local"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://my-mysql-server.example.com:3306/my_db"
        username="my_user"
        password="my_password"

        maxActive="20"
    />
</Context>

并在我的运行时类路径中拥有最新的 MySQL/JDBC 驱动程序WEB-INF/lib/mysql-jdbc-5.1.25.jar)。此外,因为这是一个全局的context.xml,所以我在${TOMCAT_HOME}/lib 也有相同的 MySQL/JDBC 驱动程序。

我得到以下堆栈跟踪:

08:54:52,905 |-ERROR in ch.qos.logback.classic.db.DBAppender[logManager-dbAppender] - problem appending event com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'caller_filename' cannot be null
    at com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'caller_filename' cannot be null
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at  at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at  at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at  at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
    at  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
    at  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
    at  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
    at  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
    at  at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at  at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at  at ch.qos.logback.classic.db.DBAppender.subAppend(DBAppender.java:105)
    at  at ch.qos.logback.classic.db.DBAppender.subAppend(DBAppender.java:42)
    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:272)
    at  at ch.qos.logback.classic.Logger.callAppenders(Logger.java:259)
    at  at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:441)
    at  at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:395)
    at  at ch.qos.logback.classic.Logger.info(Logger.java:599)
    at  at com.myapp.server.DummyServlet.doGet(Unknown Source)
    at  at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at  at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at  at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at  at java.lang.Thread.run(Thread.java:662)

关于为什么我得到 MySQL 异常的任何想法?我在 Old Nabble 上发现了类似的未回答问题,我想知道这是否是一个长期存在的错误......?

logging_event.caller_filename当(根据 Logback DBAppender 的 onw DDL)该列上不允许NULL 时,它似乎正在连接到我的 MySQL 服务器并插入 NULL 。

提前致谢!

4

1 回答 1

0

我在 PostgreSQL 和 logback 1.0.7 和 slf4j 1.6.6 上遇到过类似的问题(如果 logback 配置中没有 debug="true" ,你甚至都不知道)。

作为一种虚拟解决方案,可以放宽日志记录表中的数据库列约束(不需要调用者列中的“非空”)。然而,就我而言,事实证明我如何在代码中构造记录器实例很重要:虽然这导致了上述失败

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Logger log = LoggerFactory.getLogger(My.class);

这工作正常(即填写来电者信息)

Logger log = LoggerFactory.getLogger(My.class.getName());

虽然我无法解释。

于 2014-02-12T07:54:32.323 回答