Liquibase单元测试中liquibase.logLevel设置不生效问题求助
问题:Liquibase logLevel在单元测试中无法生效
在单元测试中无论如何设置,liquibase.logLevel始终无法生效,请求解决建议。
测试代码
@Test public void testChangeLog() { try (Connection appUserConnection = getAppUserConnection()) { Map<String, Object> config = new HashMap<>(); config.put("liquibase.logLevel", "INFO"); config.put("liquibase.liquibaseSchemaName", liquibaseSchema); Scope.child(config, () -> { Liquibase liquibase = new Liquibase(getChangeLogFile(), new ClassLoaderResourceAccessor(), new JdbcConnection(appUserConnection)); liquibase.updateTestingRollback(getContexts()); }); } catch (Exception e) { throw new RuntimeException(e); } }
POM依赖配置
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.36</version> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.4.5</version> <scope>test</scope> </dependency> <dependency> <groupId>com.mattbertolini</groupId> <artifactId>liquibase-slf4j</artifactId> <version>4.1.0</version> <scope>test</scope> </dependency>
解决建议
- 清理SLF4J绑定冲突:pom中同时引入了
slf4j-simple和logback-classic,二者都是SLF4J的日志实现,会导致绑定冲突。保留其中一个即可,推荐保留logback-classic(功能更完善),删除slf4j-simple依赖。同时确保logback-classic版本与slf4j-api版本兼容(当前1.4.5与2.0.6是兼容的)。 - 修正Scope配置键名:Liquibase的
Scope内部配置不需要前缀,将config.put("liquibase.logLevel", "INFO")改为config.put("logLevel", "INFO")即可,系统属性才需要加liquibase.前缀。 - 匹配liquibase-slf4j版本:
liquibase-slf4j的版本需要与你使用的Liquibase核心版本保持一致(比如Liquibase 4.x对应liquibase-slf4j4.x系列),旧版本可能不支持通过Scope设置日志级别的逻辑。 - 直接通过日志框架配置:如果上述方法无效,直接在
src/test/resources/logback-test.xml中配置Liquibase的日志级别,绕过Liquibase内部配置:<configuration> <logger name="liquibase" level="INFO"/> <root level="INFO"/> </configuration> - 确认Liquibase实例创建时机:确保所有需要调整日志级别的Liquibase实例都在
Scope.child的lambda内部创建,避免在Scope外部创建实例导致配置不生效。
内容的提问来源于stack exchange,提问作者Victor Rodriguez
相关产品推荐
相关产品推荐

