logback日志到db没有异常,但不工作

tcomlyy6  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(348)

我想找一个数据库和带有logback的控制台。
根据文件,我需要这些表格:

CREATE TABLE mybb.logging_event (
    timestamp BIGINT,
    formatted_message TEXT,
    logger_name VARCHAR(255),
    level_string VARCHAR(255),
    reference_flag SMALLINT,
    caller_filename VARCHAR(255),
    caller_class VARCHAR(255),
    caller_method VARCHAR(255),
    caller_line CHAR,
    event_id INT
);

CREATE TABLE mybb.logging_event_property  (
    event_id INT,
    mapped_key VARCHAR(255),
    mapped_value TEXT
);

CREATE TABLE mybb.logging_event_exception (
    event_id INT,
    i SMALLINT,
    trace_line VARCHAR(255)
);

根据这篇文章,我需要这个logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <appender name="db" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
            <driverClass>org.mariadb.jdbc.Driver</driverClass>
            <url>jdbc:mariadb://localhost:3306/mybb</url>
            <user>root</user>
            <password>root</password> <!-- no password -->
        </connectionSource>
    </appender>

    <!-- the level of the root level is set to DEBUG by default. -->
    <root>
        <!--appender-ref ref="stdout" /-->
        <appender-ref ref="db" />
        <appender-ref ref="stdout"/>
        <level value="debug" />
    </root>
</configuration>

这个 ConsoleAppender 工作,但 DBAppender 一点用都没有。在控制台或其他地方没有异常-数据库是空的。我错过了什么?

jc3wubiy

jc3wubiy1#

我用的那个医生好像过时了。在对正确的回购进行了长时间的查看之后,我发现这里有一个美丽的地方:


# 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 MySQL databases. It has been tested on MySQL 5.1.37

# on Linux

BEGIN;
DROP TABLE IF EXISTS logging_event_property;
DROP TABLE IF EXISTS logging_event_exception;
DROP TABLE IF EXISTS logging_event;
COMMIT;

BEGIN;
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 NOT NULL AUTO_INCREMENT PRIMARY KEY
  );
COMMIT;

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

BEGIN;
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)
  );
COMMIT;

有了这个,它就像一个魅力。如果其他人需要不同数据库的脚本,请链接:https://github.com/qos-ch/logback/blob/master/logback-classic/src/main/resources/ch/qos/logback/classic/db/script/

相关问题