springboot默认支持logback。
碰到的坑,版本问题,导致无法实例化dbconnect类,将版本改为1.2.3即可
默认级别是INFO以上的打印,若改为DEBUG打印的数据量太多太乱。可在root level设置。
另外如果设置url,&符号需要转义。
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <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>
编写logback.xml
<?xml version="1.0" encoding="UTF-8" ?> <configuration scan="true"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <!-- 将日志写入日志文件 --> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <!-- 写到自己项目的 logs.test.log--> <!-- <file>${catalina.base}/logs/test.log</file>--> <file>../logs/test.log</file> <append>true</append><!-- 日志追加 --> <encoder> <pattern>%d{HH:mm:ss} [%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.cj.jdbc.Driver</driverClass> <url>jdbc:mysql://localhost:3306/log?serverTimezone=Asia/Shanghai</url> <user>root</user> <password>xxxxxx</password> </connectionSource> </appender> <root level="INFO"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/> <appender-ref ref="DB" /> </root> </configuration>
建表语句,建立数据库log
# Logback: the reliable, generic, fast and flexible logging framework. # Copyright (C) 1999-2010, QOS.ch. All rights reserved. # # See http://logback.qos.ch/license.html for the applicable licensing # conditions. # This SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender. # # It is intended for MySQL databases. It has been tested on MySQL 5.1.37 # on Linux 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;
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/database/244837.html