起安! 2022-07-24 23:57 采纳率: 100%
浏览 155
已结题

SpringBoot整合Log4j2记录日志到数据库

问题遇到的现象和发生背景

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文章看个遍,还是这个错误。日志可以输出到文件,但是无法输出到数据库。

我想要达到的结果

日志输出到数据库

  • 写回答

1条回答 默认 最新

  • bekote 2022-07-25 11:33
    关注

    贴一下log4j2.xml的配置?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月26日
  • 已采纳回答 7月26日
  • 创建了问题 7月24日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵