概述:
本内容均為查閱網上大牛資料,整理彙總實踐通過後形成的個人筆記,如有相同,實屬正常!!(PS:貴在分享)
1、logback使用的jar版本:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
2、logback日志管理分為文本配置和資料庫配置:
2.1、文本配置
文本配置主要用到SizeAndTimeBasedRollingPolicy,盡量不要用TimeBasedRollingPolicy和SizeBasedTriggeringPolicy組合,否則造成檔案無法正确切割;
參考配置:
<!-- 按照每天生成日志檔案 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志檔案輸出的檔案名-->
<FileNamePattern>${LOG_HOME}/taxmgr.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志檔案保留天數-->
<MaxHistory>30</MaxHistory>
<!--日志大小-->
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級别從左顯示5個字元寬度%msg:日志消息,%n是換行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
2.2、資料庫配置
需要建立資料庫表,MySql腳本如下:
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;
mysql的資料庫連結我用的是:alibaba.druid,參考配置如下:
<!--日志異步到資料庫 -->
<appender name="logDB" class="ch.qos.logback.classic.db.DBAppender">
<!--日志異步到資料庫 -->
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<!--連接配接池 -->
<dataSource class="com.alibaba.druid.pool.DruidDataSource">
<driverClassName>com.mysql.jdbc.Driver</driverClassName>
<url>${LOGDB_URL}</url>
<username>root</username>
<password>root</password>
</dataSource>
</connectionSource>
</appender>
完整的配置,參考如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<!--定義日志檔案的存儲位址 勿在 LogBack 的配置中使用相對路徑-->
<property name="LOG_HOME" value="/test/log" />
<property name="LOGDB_URL" value="jdbc:mysql://127.0.0.1:3306/logdb?useSSL=false&characterEncoding=UTF-8" />
<!-- 控制台輸出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級别從左顯示5個字元寬度%msg:日志消息,%n是換行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- 按照每天生成日志檔案 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志檔案輸出的檔案名-->
<FileNamePattern>${LOG_HOME}/taxmgr.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志檔案保留天數-->
<MaxHistory>30</MaxHistory>
<!--日志大小-->
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級别從左顯示5個字元寬度%msg:日志消息,%n是換行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!--日志異步到資料庫 -->
<appender name="logDB" class="ch.qos.logback.classic.db.DBAppender">
<!--日志異步到資料庫 -->
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<!--連接配接池 -->
<dataSource class="com.alibaba.druid.pool.DruidDataSource">
<driverClassName>com.mysql.jdbc.Driver</driverClassName>
<url>${LOGDB_URL}</url>
<username>root</username>
<password>root</password>
</dataSource>
</connectionSource>
</appender>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="TRACE" />
<!--myibatis log configure-->
<logger name="com.apache.ibatis" level="TRACE"/>
<logger name="java.sql.Connection" level="DEBUG"/>
<logger name="java.sql.Statement" level="DEBUG"/>
<logger name="java.sql.PreparedStatement" level="DEBUG"/>
<!-- 開發、測試環境 DEBUG INFO-->
<springProfile name="dev,test,local">
<logger name="org.springframework.web" level="ERROR"/>
<logger name="com.szdbgo" level="IFNO" />
</springProfile>
<!-- 生産環境 -->
<springProfile name="pro">
<logger name="org.springframework.web" level="IFNO"/>
<logger name="com.szdbgo" level="DEBUG" />
</springProfile>
<!-- 日志輸出級别 -->
<root level="INFO">
<!-- <appender-ref ref="STDOUT" />-->
<appender-ref ref="FILE" />
<!--<appender-ref ref="logDB" />-->
</root>
</configuration>