baeldung 2025-01-03
1. 概述
在本教程中,我们将学习如何在运行 Spring Boot 应用程序的测试时设置日志级别。
虽然在测试通过时我们通常可以忽略日志,但在需要诊断失败的测试时,选择合适的日志级别就显得至关重要。
2. 日志级别的重要性
正确配置日志级别可以为我们节省大量时间。
例如,如果测试在 CI 服务器上失败,但在本地开发机器上却能通过,那么如果没有足够的日志输出,我们就无法诊断失败原因。相反,如果日志信息过于冗长,也可能难以从中找到有用的信息。
为了获得恰到好处的日志详细程度,我们可以对应用程序中不同 Java 包的日志级别进行精细调整。例如,如果某个包对测试特别关键,我们可以将其日志级别设为 DEBUG;而对于不太重要的包,我们可以将其日志级别设为 INFO 或 ERROR,以减少日志中的“噪音”。
接下来,我们将探索多种设置日志级别的方法。
3. 在 application.properties 中设置日志
如果想在测试中修改日志级别,可以在 src/test/resources/application.properties 文件中添加如下属性:
logging.level.com.baeldung.testloglevel=DEBUG
该属性将只为 com.baeldung.testloglevel 包设置日志级别。
同样,也可以通过设置根日志级别来更改所有包的日志级别:
logging.level.root=INFO
现在,我们通过添加一个会输出日志的 REST 端点来验证这些设置:
@RestController
public class TestLogLevelController {
private static final Logger LOG = LoggerFactory.getLogger(TestLogLevelController.class);
@Autowired
private OtherComponent otherComponent;
@GetMapping("/testLogLevel")
public String testLogLevel() {
LOG.trace("This is a TRACE log");
LOG.debug("This is a DEBUG log");
LOG.info("This is an INFO log");
LOG.error("This is an ERROR log");
otherComponent.processData();
return "Added some log output to console...";
}
}
正如预期,在测试中调用该端点时,我们将看到来自 TestLogLevelController 的 DEBUG 日志:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
这种方式设置日志级别非常简单,如果你的测试使用了 @SpringBootTest 注解,这无疑是首选方式。但如果你没有使用该注解,则需要采用其他方式配置日志级别。
3.1 基于 Profile 的日志设置
虽然将日志设置放在 src/test/resources/application.properties 中适用于大多数情况,但有时我们可能希望为某个测试或一组测试使用不同的日志配置。
此时,可以使用 @ActiveProfiles 注解为测试指定一个 Spring Profile:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
@ActiveProfiles("logging-test")
public class TestLogLevelWithProfileIntegrationTest {
// ...
}
然后,在 src/test/resources 目录下创建一个名为 application-logging-test.properties 的文件,并在其中定义日志设置:
logging.level.com.baeldung.testloglevel=TRACE
logging.level.root=ERROR
使用上述配置调用 TestLogLevelController 时,我们会看到控制器的 TRACE 日志,而其他包将不再输出 INFO 日志:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
4. 配置 Logback
Spring Boot 默认使用 Logback 作为日志框架。我们可以在 src/test/resources 目录下创建 logback-test.xml 文件来设置日志级别:
<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>
<logger name="com.baeldung.testloglevel" level="debug"/>
</configuration>
上述示例将根日志级别设为 ERROR,并将 com.baeldung.testloglevel 包的日志级别设为 DEBUG。
应用该配置后,日志输出如下:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
4.1 基于 Profile 的 Logback 配置
另一种为测试设置特定 Profile 配置的方法是在 application.properties 中指定 logging.config 属性:
logging.config=classpath:logback-testloglevel.xml
或者,如果希望在 classpath 中只保留一个 Logback 配置文件,可以在 logback.xml 中使用 <springProfile> 元素:
<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>
<springProfile name="logback-test1">
<logger name="com.baeldung.testloglevel" level="info"/>
</springProfile>
<springProfile name="logback-test2">
<logger name="com.baeldung.testloglevel" level="trace"/>
</springProfile>
</configuration>
当使用 logback-test1 Profile 调用 TestLogLevelController 时,输出如下:
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
而如果切换到 logback-test2 Profile,输出则变为:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
5. 使用 Log4j2 的替代方案
如果使用的是 Log4j2,可以在 src/test/resources 目录下创建 log4j2-spring.xml 文件来设置日志级别:
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.baeldung.testloglevel" level="debug" />
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
还可以通过在 application.properties 中设置 logging.config 属性来指定 Log4j 配置文件路径:
logging.config=classpath:log4j-testloglevel.xml
应用上述配置后的日志输出如下:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
6. 结论
在本文中,我们学习了在测试 Spring Boot 应用程序时如何设置日志级别,并探索了多种配置方式。
在 Spring Boot 的 application.properties 中设置日志级别是最简单的方式,尤其是在使用 @SpringBootTest 注解时。