首页 > 解决方案 > 如何仅在春季更改单元测试执行的日志级别

问题描述

我对如何仅更改单元测试执行的日志级别有疑问。

目前,我的应用程序正在以正确的日志级别运行(执行,而不是单元测试),一切都很好,非常高兴。

但是,每次我在任何本地机器上或在我们的 CI 管道上运行单元测试时,我们都会看到以下内容:

10:29:45.274 [main] DEBUG org.springframework.test.util.ReflectionTestUtils - Setting field 'contactPoints' of type [null] on target object [CassandraConfiguration@231f98ef] or target class [class CassandraConfiguration] to value [localhost]
10:29:45.277 [main] DEBUG org.springframework.test.util.ReflectionTestUtils - Setting field 'port' of type [null] on target object [CassandraConfiguration@231f98ef] or target class [class com.apple.pay.cloud.vaxholm.base.configuration.CassandraConfiguration] to value [9042]

和其他调试日志。

什么是“禁用”这些调试日志的正确方法,或者仅针对单元测试“更改​​日志级别”?

谢谢

标签: javaspringlogging

解决方案


如果您没有更改您正在使用 logback 的 spring 的默认记录器...

如果是这样的话

在中创建一个logback-test.xml文件src/test/resources

在该文件中,您应该能够配置您的记录器(很可能是标准输出记录器

就像是:

<configuration>
    <include resource="/org/springframework/boot/logging/logback/base.xml"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <root level="error">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

推荐阅读