如何在@SpringBootTest期间跳过JdbcAppender?

时间:2018-06-15 06:36:49

标签: spring-boot junit log4j2 spring-boot-test

我们正在使用JdbcAppender将所有WARN和ERROR日志记录到数据库中。 但是当我们运行SpringBootTest类时,我们正在使用内存数据库进行测试,因此由于JdbcAppender而导致抛出异常无法记录。

@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);
}

方法:

AGAINST ('branch*' IN BOOLEAN MODE);

在@SpringBootTest期间有没有办法跳过jdbcAppender?

0 个答案:

没有答案
相关问题