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

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日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装