为什么我无法重用Logback的DBAppender?

时间:2013-07-18 20:55:19

标签: java logback

我正在使用logback的DBAppender将日志消息输出到PostgreSQL数据库。这适用于我实现它的初始应用程序,但是如果我将我的配置复制到另一个也使用logback的程序,它就不起作用。为什么?

其他程序不会引发任何错误,执行(和日志记录)继续,就好像没有对logback.xml文件进行任何更改一样。

我的配置如下:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{35}) - %msg %n</pattern>
        </encoder>
    </appender>

    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource
                class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource
                    class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <driverClass>org.postgresql.Driver</driverClass>
                <jdbcUrl>jdbc:postgresql://ec2-xxx-21-xxx-162.compute-1.amazonaws.com:6232/mydatabase?user=myuser&amp;password=mypw&amp;ssl=true&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory</jdbcUrl>
            </dataSource>
        </connectionSource>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="DB" />
    </root>
</configuration>

使用PostgreSQL脚本创建表:

    -- 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.classic.db.DBAppender
--
-- It is intended for PostgreSQL databases.

DROP TABLE    logging_event_property;
DROP TABLE    logging_event_exception;
DROP TABLE    logging_event;
DROP SEQUENCE logging_event_id_seq;


CREATE SEQUENCE logging_event_id_seq MINVALUE 1 START 1;


CREATE TABLE logging_event 
  (
    timestmp         BIGINT NOT NULL,
    formatted_message  TEXT NOT NULL,
    logger_name       VARCHAR(254) NOT NULL,
    level_string      VARCHAR(254) NOT NULL,
    thread_name       VARCHAR(254),
    reference_flag    SMALLINT,
    arg0              VARCHAR(254),
    arg1              VARCHAR(254),
    arg2              VARCHAR(254),
    arg3              VARCHAR(254),
    caller_filename   VARCHAR(254) NOT NULL,
    caller_class      VARCHAR(254) NOT NULL,
    caller_method     VARCHAR(254) NOT NULL,
    caller_line       CHAR(4) NOT NULL,
    event_id          BIGINT DEFAULT nextval('logging_event_id_seq') PRIMARY KEY
  );

CREATE TABLE logging_event_property
  (
    event_id          BIGINT NOT NULL,
    mapped_key        VARCHAR(254) NOT NULL,
    mapped_value      VARCHAR(1024),
    PRIMARY KEY(event_id, mapped_key),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  );

CREATE TABLE logging_event_exception
  (
    event_id         BIGINT NOT NULL,
    i                SMALLINT NOT NULL,
    trace_line       VARCHAR(254) NOT NULL,
    PRIMARY KEY(event_id, i),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  );

1 个答案:

答案 0 :(得分:0)

原来问题不在于logback,因为创建一个新项目工作正常。

在这种情况下,似乎忽略了logback配置,log4j正在提供日志记录。