问题遇到的现象和发生背景
springboot整合log4j2,使用jdbcAppender记录日志到数据库,程序启动时异常
问题相关代码,请勿粘贴截图
package com.example.demo.util;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
@author caohongyun
/
public class ConnectionFactoryConfig{private static interface Singleton{
final ConnectionFactoryConfig INSTANCE = new ConnectionFactoryConfig();
}
private HikariDataSource dataSource;
private ConnectionFactoryConfig(){
//也可以使用配置文件直接加载
// HikariConfig config = new HikariConfig("application.properties");
// this.dataSource = new HikariDataSource(config);String user = "root"; String password = "admin"; String url = "jdbc:mysql://localhost:3306/px?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"; String driverClassName = "com.mysql.cj.jdbc.Driver"; HikariConfig config = new HikariConfig(); config.setDriverClassName(driverClassName); config.setJdbcUrl(url); config.setUsername(user); config.setPassword(password); config.addDataSourceProperty("cachePrepStmts","true"); config.addDataSourceProperty("prepstmtCacheSize","250"); config.addDataSourceProperty("prepstmtCacheSqlLimit","2048"); //设置连接超时为8小时 config.setConnectionTimeout(8*60*60); this.dataSource = new HikariDataSource(config);
}
public static Connection getDatabaseConnection() throws SQLException{
System.out.println("获取数据库连接重要!!!"+Singleton.INSTANCE.dataSource.getConnection()); return Singleton.INSTANCE.dataSource.getConnection();
}
}
运行结果及报错内容
2022-07-24 23:52:32,186 main ERROR Unable to write to database [JdbcManager{name=databaseAppender, bufferSize=0, tableName=log, columnConfigs=[{ name=log_id, layout=%X{logId}, literal=null, timestamp=false }, { name=executor, layout=%X{executor}, literal=null, timestamp=false }, { name=execute_time, layout=%X{executeTime}, literal=null, timestamp=false }, { name=method_name, layout=%X{methodName}, literal=null, timestamp=false }, { name=url, layout=%X{url}, literal=null, timestamp=false }, { name=status, layout=%X{status}, literal=null, timestamp=false }, { name=message, layout=%X{message}, literal=null, timestamp=false }], columnMappings=[]}] for appender [databaseAppender]. org.apache.logging.log4j.core.appender.AppenderLoggingException: Cannot write logging event; JDBC manager not connected to the database, running=true, [columnConfigs=[{ name=log_id, layout=%X{logId}, literal=null, timestamp=false }, { name=executor, layout=%X{executor}, literal=null, timestamp=false }, { name=execute_time, layout=%X{executeTime}, literal=null, timestamp=false }, { name=method_name, layout=%X{methodName}, literal=null, timestamp=false }, { name=url, layout=%X{url}, literal=null, timestamp=false }, { name=status, layout=%X{status}, literal=null, timestamp=false }, { name=message, layout=%X{message}, literal=null, timestamp=false }], sqlStatement=insert into log (log_id,executor,execute_time,method_name,url,status,message) values (?,?,?,?,?,?,?), factoryData=FactoryData [connectionSource=factory{ public static java.sql.Connection com.example.demo.util.Log4j2ConnectionFactory.getDruidConnection() }, tableName=log, columnConfigs=[{ name=log_id, layout=%X{logId}, literal=null, timestamp=false }, { name=executor, layout=%X{executor}, literal=null, timestamp=false }, { name=execute_time, layout=%X{executeTime}, literal=null, timestamp=false }, { name=method_name, layout=%X{methodName}, literal=null, timestamp=false }, { name=url, layout=%X{url}, literal=null, timestamp=false }, { name=status, layout=%X{status}, literal=null, timestamp=false }, { name=message, layout=%X{message}, literal=null, timestamp=false }], columnMappings=[], immediateFail=false, retry=true, reconnectIntervalMillis=5000, truncateStrings=true], connection=null, statement=null, reconnector=Reconnector [latch=java.util.concurrent.CountDownLatch@aac3f4e[Count = 0], shutdown=false], isBatchSupported=true, columnMetaData=null]).
我的解答思路和尝试过的方法
CSDN上JdbcAppender文章看个遍,还是这个错误。日志可以输出到文件,但是无法输出到数据库。
我想要达到的结果
日志输出到数据库