如何配置logback使日志输出到mysql数据库

导读:本篇文章讲解 如何配置logback使日志输出到mysql数据库,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1、引入logback pom配置

<logback.version>1.0.13</logback.version>

<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-core</artifactId>
	<version>${logback.version}</version>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-classic</artifactId>
	<version>${logback.version}</version>
</dependency>

2、配置logback.xml

<appender name="PROJECT-TASK" class="ch.qos.logback.classic.db.DBAppender">  
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">  
            <dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource">                 
                <driverClass>net.sf.log4jdbc.DriverSpy</driverClass>  
                <url>jdbc:log4jdbc:mysql://${ip}:${port}/${dbname}</url>  
                <user>${user}</user>  
                <password>${password}</password>
                <sqlDialect class="ch.qos.logback.core.db.dialect.MySQLDialect" />   
            </dataSource>  
        </connectionSource>
    </appender> 

….

<root level="INFO">
        <appender-ref ref="PROJECT-TASK"/>
    </root>

3、创建logback的三张表 

例如 1.0.13版本的mysql脚本

https://github.com/qos-ch/logback/blob/v_1.0.13/logback-classic/src/main/java/ch/qos/logback/classic/db/script/mysql.sql

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://www.bmabk.com/index.php/post/9988.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!