建e设计网,江门seo哪家专业,大众软件回应中国芯片行业最大投资,网站开发 认证在系统中采用了spring boot logback#xff0b;slf4j的日志框架#xff0c;将系统日志记录到数据库。
相关参考来源#xff1a; 官方文档-DBAppender Logback输出日志到自定义MySQL数据库#xff08;重写DBAppender#xff09; logback日志框架中filter的使用 1. 添加依…在系统中采用了spring boot logbackslf4j的日志框架将系统日志记录到数据库。
相关参考来源 官方文档-DBAppender Logback输出日志到自定义MySQL数据库重写DBAppender logback日志框架中filter的使用 1. 添加依赖
从 logback 版本 1.2.8 开始DBAppender 不再随 logback-classic 一起提供所以需要单独引入
dependencygroupIdch.qos.logback.db/groupIdartifactIdlogback-classic-db/artifactIdversion1.2.11.1/version
/dependency2. 自定义表结构:
可以根据实际情况增加或减少和修改字段
CREATE TABLE t_log_logback (id VARCHAR ( 64 ) NOT NULL COMMENT 主键,create_time VARCHAR ( 32 ) NOT NULL COMMENT 创建时间,message TEXT NOT NULL COMMENT 内容,level_string VARCHAR ( 254 ) NOT NULL COMMENT 日志等级TRACE,DEBUG,INFO,WARNING,ERROR,FATAL,logger_name VARCHAR ( 254 ) NOT NULL COMMENT 发出日志记录请求的记录器的名称,thread_name VARCHAR ( 254 ) COMMENT 线程名称,reference_flag INT ( 11 ) COMMENT MDC属性,caller_filename VARCHAR ( 254 ) NOT NULL COMMENT 发出日志记录请求的文件名,caller_class VARCHAR ( 254 ) NOT NULL COMMENT 发出日志记录请求的类,caller_method VARCHAR ( 254 ) NOT NULL COMMENT 发出日志记录请求的方法的名称,caller_line VARCHAR ( 4 ) NOT NULL COMMENT 发出日志记录请求的行号,
PRIMARY KEY ( id ) USING BTREE
) ENGINE INNODB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_bin ROW_FORMAT DYNAMIC COMMENT logback系统日志记录表;3. 自定义追加器DbLogbackAppender
import ch.qos.logback.classic.spi.CallerData;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.db.DBAppenderBase;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;/*** p* 自定义DB日志追加器参考实现类 {link ch.qos.logback.classic.db.DBAppender}* 参考来源https://blog.csdn.net/qq_20914913/article/details/92830914* /p*/
public class DbLogbackAppender extends DBAppenderBaseILoggingEvent {private String insertSQL;private static final Method GET_GENERATED_KEYS_METHOD;// 对应于数据库字段的插入数据序号private static final int ID_INDEX 1;private static final int CREATE_TIME_INDEX 2;private static final int MESSAGE_INDEX 3;private static final int LEVEL_STRING_INDEX 4;private static final int LOGGER_NAME_INDEX 5;private static final int THREAD_NAME_INDEX 6;private static final int REFERENCE_FLAG_INDEX 7;private static final int CALLER_FILENAME_INDEX 8;private static final int CALLER_CLASS_INDEX 9;private static final int CALLER_METHOD_INDEX 10;private static final int CALLER_LINE_INDEX 11;private static final StackTraceElement EMPTY_CALLER_DATA CallerData.naInstance();// 处理主键的自动生成这里我们使用手工生成因此下面代码可忽略static {// PreparedStatement.getGeneratedKeys() method was added in JDK 1.4Method getGeneratedKeysMethod;try {// thegetGeneratedKeysMethod PreparedStatement.class.getMethod(getGeneratedKeys, (Class[]) null);} catch (Exception ex) {getGeneratedKeysMethod null;}GET_GENERATED_KEYS_METHOD getGeneratedKeysMethod;}Overridepublic void start() {insertSQL buildInsertSQL();cnxSupportsBatchUpdates connectionSource.supportsBatchUpdates();// super.start();super.started true;}// 核心代码构建插入语句并对应数据库字段private static String buildInsertSQL() {return INSERT INTO t_log_logback (id, create_time, message, level_string, logger_name, thread_name, reference_flag, caller_filename, caller_class, caller_method, caller_line) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);}// 管理每个字段插入的数据private void bindLoggingEventWithInsertStatement(PreparedStatement stmt, ILoggingEvent event) throws SQLException {// TODO 手工处理ID的生成stmt.setString(ID_INDEX, IdUtils.simpleUUID());stmt.setString(CREATE_TIME_INDEX, LocalDateTime.ofInstant(Instant.ofEpochMilli(event.getTimeStamp()),ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss)));stmt.setString(MESSAGE_INDEX, event.getFormattedMessage());stmt.setString(LEVEL_STRING_INDEX, event.getLevel().toString());stmt.setString(LOGGER_NAME_INDEX, event.getLoggerName());stmt.setString(THREAD_NAME_INDEX, event.getThreadName());}// 管理每个字段插入的数据private void bindCallerDataWithPreparedStatement(PreparedStatement stmt, StackTraceElement[] callerDataArray) throws SQLException {StackTraceElement caller extractFirstCaller(callerDataArray);stmt.setInt(REFERENCE_FLAG_INDEX, 0);stmt.setString(CALLER_FILENAME_INDEX, caller.getFileName());stmt.setString(CALLER_CLASS_INDEX, caller.getClassName());stmt.setString(CALLER_METHOD_INDEX, caller.getMethodName());stmt.setString(CALLER_LINE_INDEX, Integer.toString(caller.getLineNumber()));}// 核心方法插入具体日志数据Overrideprotected void subAppend(ILoggingEvent event, Connection connection, PreparedStatement insertStatement) throws Throwable {bindLoggingEventWithInsertStatement(insertStatement, event);// This is expensive... should we do it every time?bindCallerDataWithPreparedStatement(insertStatement, event.getCallerData());int updateCount insertStatement.executeUpdate();if (updateCount ! 1) {addWarn(Failed to insert loggingEvent);}}private StackTraceElement extractFirstCaller(StackTraceElement[] callerDataArray) {StackTraceElement caller EMPTY_CALLER_DATA;if (hasAtLeastOneNonNullElement(callerDataArray))caller callerDataArray[0];return caller;}private boolean hasAtLeastOneNonNullElement(StackTraceElement[] callerDataArray) {return callerDataArray ! null callerDataArray.length 0 callerDataArray[0] ! null;}Overrideprotected Method getGeneratedKeysMethod() {return GET_GENERATED_KEYS_METHOD;}Overrideprotected String getInsertSQL() {return insertSQL;}protected void secondarySubAppend(ILoggingEvent event, Connection connection, long eventId) throws Throwable {}Overrideprotected long selectEventId(PreparedStatement insertStatement, Connection connection) throws SQLException, InvocationTargetException {return 0;}
}
4. logback-spring.xml配置关联
添加自定义的追加器DbLogbackAppender
?xml version1.0 encodingUTF-8?
configuration debugfalse!-- 获取springboot中的数据库配置 --springProperty scopecontext namedriverClassName sourcespring.datasource.master.driverClassName defaultValuedriverClassName/springProperty scopecontext nameurl sourcespring.datasource.master.url defaultValueurl/springProperty scopecontext nameusername sourcespring.datasource.master.username defaultValueusername/springProperty scopecontext nameencryptPassword sourcespring.datasource.master.encryptPassword defaultValueencryptPassword/!-- 添加自定义DbLogbackAppender --appender nameDB classcom.xxx.DbLogbackAppenderconnectionSource classch.qos.logback.core.db.DriverManagerConnectionSourcedriverClass${driverClassName}/driverClassurl${url}/urluser${username}/userpassword${password}/password/connectionSource!--临界值过滤。只记录指定级别以及高于该级别的日志--filter classch.qos.logback.classic.filter.ThresholdFilterlevelWARN/level/filter/appenderroot levelDEBUGEappender-ref refDB //root
/configuration到这里就配置完成了重启系统访问日志会将warn级别以上的日志记录到数据库表。
由于数据源是使用logback的LogbackConnectionSource如果要自定义数据源请往下看
5.自定义数据源
创建类LogbackConnectionSource.java继承ConnectionSourceBase import ch.qos.logback.core.db.ConnectionSourceBase;import java.sql.Connection;
import java.sql.SQLException;/*** p* 获取数据库连接用于logback并提供DbLogbackAppender使用* 参考类ch.qos.logback.core.db.DriverManagerConnectionSource* /p*/
public class LogbackConnectionSource extends ConnectionSourceBase {public Connection getConnection() {获取数据库连接Connection connection null;// 自行构建连接// ...return connection;}
}修改logback-spring.xml配置为如下 !-- 添加自定义DbLogbackAppender --appender nameDB classcom.xxx.DbLogbackAppender!-- 添加自定义的数据源 --connectionSource classcom.xxx.LogbackConnectionSource!-- driverClass${driverClassName}/driverClassurl${url}/urluser${username}/userpassword${password}/password--/connectionSource!--临界值过滤。只记录指定级别以及高于该级别的日志--filter classch.qos.logback.classic.filter.ThresholdFilterlevelWARN/level/filter/appender