0

使用logback-1.0.13.jar.

用于创建所需表的 DDL 脚本DBAppender位于该 JAR 内:

logback-1.0.13/logback-access/src/main/java/ch/qos/logback/ access /db/script/mysql.sql

这个文件的内容是:

# Logback: the reliable, generic, fast and flexible logging framework.
# Copyright (C) 1999-2010, QOS.ch. All rights reserved.
#
# See http://logback.qos.ch/license.html for the applicable licensing 
# conditions.

# This SQL script creates the required tables by ch.qos.logback.access.db.DBAppender.
#
# It is intended for MySQL databases. It has been tested on MySQL 5.0.22 with 
# INNODB tables.


BEGIN;
DROP TABLE IF EXISTS access_event_header;
DROP TABLE IF EXISTS access_event;
COMMIT;

BEGIN;
CREATE TABLE ACCESS_EVENT 
(
timestmp          BIGINT NOT NULL,
requestURI        VARCHAR(254),
requestURL        VARCHAR(254),
remoteHost        VARCHAR(254),
remoteUser        VARCHAR(254),
remoteAddr        VARCHAR(254),
protocol          VARCHAR(254),
method            VARCHAR(254),
serverName        VARCHAR(254),
postContent       VARCHAR(254),
event_id          BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
COMMIT;

BEGIN;
CREATE TABLE access_event_header
(
event_id          BIGINT NOT NULL,
header_key        VARCHAR(254) NOT NULL,
header_value      VARCHAR(1024),
PRIMARY KEY(event_id, header_key),
FOREIGN KEY (event_id) REFERENCES access_event(event_id)
);
COMMIT;

我在我的 MySQL 服务器上运行了这个脚本,它成功执行,创建了 access_eventaccess_event_header

我的logback-test.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.DriverManagerConnectionSource">
            <driverClass>com.mysql.jdbc.Driver</driverClass>
            <url>jdbc:mysql://mysql.server.example.com:3306/my_db</url>
            <user>my_user</user>
            <password>my_password</password>
        </connectionSource>
    </appender>

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

我使用 Logback 的 Java 驱动程序:

public class LogbackTester {
    private Logger logger = LoggerFactory.getLogger(LogbackTester.class);

    public static void main(String[] args) {
        LogbackTester tester = new LogbackTester();
        tester.run();
    }

    private void run() {
        logger.debug("Logging a DEBUG message.");
        logger.trace("Logging a TRACE message.");
        logger.info("Logging an INFO message.");
        logger.warn("Logging a WARN message.");
        logger.error("Logging an ERROR message.");
    }
}

当那个 Java 执行时,我得到这个控制台输出:

22:09:55,266 |-ERROR in ch.qos.logback.classic.db.DBAppender[logManager-dbAppender] - problem appending event com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'my_db.logging_event' doesn't exist
    at com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'my_db.logging_event' doesn't exist
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at  at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    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:1053)
    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 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.error(Logger.java:542)
    at  at com.dummylandapp.server.LogbackTester.run(LogbackTester.java:19)
    at  at com.dummylandapp.server.LogbackTester.main(LogbackTester.java:11)

为什么要找logging_event桌子?1.0.13 是否附带了错误的 DDL 脚本?如果是这样,与 1.0.13 一起使用的正确 DDL 是什么DBAppender

4

1 回答 1

0

您将logback-accesslogback-classic混淆了。在“logback-1.0.13/logback- classic /src/main/java/...”下搜索 mysql.sql 并使用该文件创建表。

Logback-access 用于与 Jetty 或 Tomcat 等 Web 服务器一起记录 (HTTP) 访问事件。原生实现 SLF4J API 的 Logback-classic 用于开发人员日志记录。

于 2013-05-30T08:00:08.523 回答