logback系列之五:输出日志到数据库-DBAppender生成的表

SQL脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;

XML配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <property name="LOG_HOME" value="/home/owinfo/log" />

    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d - %msg%n</pattern>
        </layout>
    </appender>
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>%d - %msg%n%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/service-***-info.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
    </appender>
    <!--日志异步到数据库 -->
    <appender name="mysqlLog" class="ch.qos.logback.classic.db.DBAppender">
        <!-- 过滤器,只记录ERROR级别的日志 -->
        <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.cj.jdbc.Driver</driverClass>
            <url>jdbc:mysql://192.168.0.1:3306/kuming?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false</url>
            <user>root</user>
            <password>12345</password>
        </connectionSource>
    </appender>
    <root level="info">
        <appender-ref ref="mysqlLog" />
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog" />
    </root>
</configuration>

try{}catch(){}中记录:
logger.error(“删除异常—–》”+e.getMessage());

ˆ Back To Top