logback异步将日志导入到1文件_2数据库中

前言

1、日志输入到数据库中

1、创建数据库表



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


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
)


CREATE TABLE logging_event_property
(
event_id          BIGINT NOT NULL,
mapped_key        VARCHAR(150) NOT NULL,
mapped_value      TEXT,
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)
);



2、日志配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>


  

    <!--日志异步到数据库 -->
    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <!--日志异步到数据库-->
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
            <driverClass>com.mysql.jdbc.Driver</driverClass>
            <url>jdbc:mysql://localhost:3306/healerjean?useUnicode=true&amp;allowMultiQueries=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&amp;useSSL=false</url>
            <user>healerjean</user>
            <password>healerjean</password>
        </connectionSource>
    </appender>





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



3、查看日志

SELECT * from logging_event;

[外链图片转存失败(img-lTI4Q4yx-1566553767486)(https://raw.githubusercontent.com/HealerJean123/HealerJean123.github.io/master/blogImages/WX20180910-123004.png)]

2、日志输出到文件中

1、文件夹会自动创建,但是为了防止意外我们孩子自己建立文件夹吧

2、文件每个小时生成一个日志文件

%d{yyyy-MM-dd HH}.log

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>


    <property name="LOG_PATH" value="/Users/healerjean/Desktop/logs/youhui/log" />
    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}-youhui-%d{yyyy-MM-dd HH}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n </pattern>
        </encoder>
    </appender>
    




    <root level="info">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>


[外链图片转存失败(img-PvolKjCr-1566553767487)(https://raw.githubusercontent.com/HealerJean123/HealerJean123.github.io/master/blogImages/WX20180910-123543@2x.png)]





如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,添加博主微信哦, 请下方留言吧。可与博主自由讨论哦

支付包 微信 微信公众号
支付宝 微信 微信公众号
posted @ 2018-09-10 12:37  HealerJean  阅读(136)  评论(0编辑  收藏  举报