@SpringBootTest:预测试日志设置为默认调试?

时间:2021-07-06 14:06:34

标签: spring-boot logging

一个简单的 spring-boot 项目 (Initializr link) 生成一个 @SpringBootTest 类,该类会吐出以下 DEBUG 日志消息:

DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - ...
DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - ... 
DEBUG org.springframework.test.context.BootstrapUtils - ... 
DEBUG org.springframework.test.context.support.AbstractContextLoader - ...
DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - ...
DEBUG org.springframework.test.context.support.ActiveProfilesUtils - ...
DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - ...
DEBUG org.springframework.test.context.support.TestPropertySourceUtils - ...

这发生在实际测试类开始执行之前。

  1. 启用该日志记录的配置在哪里?
  2. 此功能在何处记录?
  3. 如何关闭它?

谢谢!

2 个答案:

答案 0 :(得分:1)

除了 @simon-martinelli 提供的信息外,此 Baeldung article 也可能有所帮助。

总而言之:您可以将 logback-test.xml 添加到测试类路径的根目录以配置日志级别以避免 DEBUG 日志或完全禁用日志记录。 :)

答案 1 :(得分:0)

SimonIngo 都提到了配置 logback,事实证明这是禁用调试消息的正确方法(最初,我放错了 logback conf 文件,这就是为什么我之前评论说它没有用。抱歉)。

src/test/resources/logback.groovy

import ch.qos.logback.classic.encoder.PatternLayoutEncoder

appender('STDOUT', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = '%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n'
    }
}
logger 'org.springframework.test', WARN
root(INFO, ["STDOUT'])

澄清:

启用调试的配置不是 spring boot的,而是logback default

<块引用>

假设配置文件 logback-test.xml 或 logback.xml 不存在,logback 将默认调用 BasicConfigurator...此外,默认情况下,root logger 被分配了 DEBUG 级别。

org.springframework.test 中的记录器(例如 BootstrapUtils)是静态初始化的,因此当应用程序上下文加载并读取 application.properties 时,这些记录器已经被构建,继承了它们的来自 ROOT 的级别,默认为 debug