如何仅在 Spring 中更改单元测试执行的日志级别

Pat*_*Pat 1 java logging spring

我有一个关于如何仅更改单元测试执行的日志级别的问题。

\n

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

\n

然而,每次我运行单元测试时,无论是在任何本地计算机上,还是在我们的 CI 管道上,我们都会看到以下内容:

\n
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]\n10: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]\n\n
Run Code Online (Sandbox Code Playgroud)\n

以及其他调试日志。

\n

\xe2\x80\x9cdisable\xe2\x80\x9d 这些调试日志或 \xe2\x80\x9c 仅更改单元测试的日志级别 \xe2\x80\x9d 的正确方法是什么?

\n

谢谢

\n

Ber*_*rka 5

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

如果是这样的话

在中创建一个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>
Run Code Online (Sandbox Code Playgroud)