如何在Springboot 3.1.10中启用单元测试的DEBUG级别?

问题描述 投票:0回答:1

我需要全局启用调试模式,因此应用程序类的输出会记录在应用程序和单元测试中。 我在

src\main\resources\application.properties
中启用了调试模式,它仅在应用程序中有效,但在测试中有效。 顺便说一句,最初由于
No SLF4J providers were found
,测试中没有任何日志记录,因此我必须将
slf4j-simple
添加到 pom.xml 来修复该问题,但测试的日志级别仍保持在 INFO 级别。 我尝试将
application.properties
复制到
src\test\resources
中,但没有帮助。 我的应用程序正在使用
java.util.logging

如有任何建议,我们将不胜感激。

spring-boot unit-testing logging junit spring-test
1个回答
0
投票

由于单元测试不在 Spring Boot 下运行并且您使用的是 slf4j-simple,因此您需要配置 Maven Surefire 以在

pom.xml
中设置正确的日志级别:

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.2.5</version>
        <configuration>   
          <systemPropertyVariables>
            <!-- logging (also src/test/resources/simplelogger.properties) -->
            <org.slf4j.simpleLogger.defaultLogLevel>DEBUG</org.slf4j.simpleLogger.defaultLogLevel>
 </systemPropertyVariables>
        </configuration>
      </plugin>

您可以在官方文档中找到其他属性来微调简单记录器:https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html

附注根据经验,每个使用 slf4j-simple 的项目最终都会切换到 Logback,除了一次性的教育练习。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.