spring-boot - 如何在@SpringBootTest 期间跳过 JdbcAppender?
问题描述
我们正在使用 JdbcAppender 将所有 WARN 和 ERROR 日志记录到数据库中。但是当我们运行 SpringBootTest 类时,我们使用内存数据库进行测试,因此由于 JdbcAppender 无法记录而引发异常。
Caused by: org.h2.jdbc.JdbcSQLException: Table "APPLICATION_LOG" not found; SQL statement:
INSERT INTO application_log (application_log_id,service_id,logger,log_level,message,throwable,log_date) VALUES (?,?,?,?,?,?,?)
2018-06-15 11:53:38,369 main ERROR An exception occurred processing Appender DB org.apache.logging.log4j.core.appender.AppenderLoggingException: Cannot write logging event or flush buffer; JDBC manager cannot connect to the database.
方法:
@PostConstruct
public void onStartUp() {
// Create a new connectionSource build from the Spring properties
LoggerConnectionSource connectionSource = new LoggerConnectionSource(url, userName, password, validationQuery);
// This is the mapping between the columns in the table and what to
// insert in it.
ColumnConfig[] columnConfigs = new ColumnConfig[7];
columnConfigs[0] = ColumnConfig.createColumnConfig(null, "application_log_id", "0", null, null, null, null);
columnConfigs[1] = ColumnConfig.createColumnConfig(null, "service_id", "" + serviceId + "", null, null, "false",
null);
columnConfigs[2] = ColumnConfig.createColumnConfig(null, "logger", "%logger", null, null, "false", null);
columnConfigs[3] = ColumnConfig.createColumnConfig(null, "log_level", "%level", null, null, "false", null);
columnConfigs[4] = ColumnConfig.createColumnConfig(null, "message", "%message", null, null, "false", null);
columnConfigs[5] = ColumnConfig.createColumnConfig(null, "throwable", "%ex{full}", null, null, "false", null);
columnConfigs[6] = ColumnConfig.createColumnConfig(null, "log_date", null, null, "true", null, null);
// filter for the appender to keep only errors
ThresholdFilter filter = ThresholdFilter.createFilter(appenderLevel, null, null);
JdbcAppender appender = JdbcAppender.createAppender(appenderName, "true", filter, connectionSource, "1",
appenderTableName, columnConfigs);
// start the appender, and this is it...
appender.start();
((Logger) LogManager.getRootLogger()).addAppender(appender);
}
有没有办法在 @SpringBootTest 期间跳过 jdbcAppender ?
解决方案
推荐阅读
- android - 为什么要求在 android 模拟器上工作但不能在 google play store 应用程序上工作?
- pandas - panda read_html 忽略
并连接字符串 - php - 当我将文件上传到服务器时,如何获取文件名?
- django - 如何使用 django google-auth 授权我的 API 请求?
- java - Docker + Springboot + postgres + JHipster = org.postgresql.util.PSQLException:连接尝试失败
- json - 如何在 JSON 中添加对象数组
- php - 将数据从 unity3d 游戏发送到 PHP 空变量
- c# - 如何检查用户是否获胜?
- swift - 如何在列表视图中更改项目的顺序?
- node.js - nodejs动态增加JSON对象属性而不会在第一次尝试时得到未定义