Springboot-logback日志管理
概述:
本内容均为查阅网上大牛资料,整理汇总实践通过后形成的个人笔记,如有相同,实属正常!!(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>