测试时在Spring Boot中设置日志级别

Setting the Log Level in Spring Boot when Testing

1.概述

在本教程中,我们将展示在为Spring Boot应用程序运行测试时如何设置日志级别。

尽管在测试通过时我们几乎可以忽略日志,但是如果需要诊断失败的测试,则选择正确的日志级别可能至关重要。

2.日志级别的重要性

正确配置日志级别可以节省大量时间。

例如,如果测试在CI服务器上失败但通过我们的开发机器,则除非有足够的日志输出,否则我们将无法诊断失败的测试。 另一方面,如果我们记录过多的详细信息,可能会很难找到有用的信息。

为了获得适当的详细信息,我们可以微调应用程序包的日志记录级别。 如果我们发现Java包对我们的测试更重要,则可以给它一个较低的级别,例如DEBUG。 同样,为了避免日志中出现过多噪音,我们可以为不太重要的软件包配置更高的级别,例如INFO或ERROR。

让我们探索设置日志记录级别的各种方法。

3.在application.properties中记录设置

如果要在测试中修改日志级别,可以在src / test / resources / application.properties中设置一个属性:

1
logging.level.com.baeldung.testloglevel=DEBUG

此属性将专门为com.baeldung.testloglevel包设置日志级别。

同样,我们可以通过设置根日志级别来更改所有软件包的日志记录级别:

1
logging.level.root=INFO

现在,让我们通过添加写一些日志的REST端点来尝试我们的日志记录设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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日志:

1
2
3
4
5
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。 基于配置文件的日志记录设置

尽管将设置放入src / test / application.properties可以在大多数情况下使用,但是在某些情况下,我们可能希望为一个测试或一组测试使用不同的设置。

在这种情况下,我们可以使用ActiveProfiles批注将Spring概要文件添加到我们的测试中:

1
2
3
4
5
6
7
8
9
@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文件中:

1
2
logging.level.com.baeldung.testloglevel=TRACE
logging.level.root=ERROR

如果我们使用描述的设置从测试中调用TestLogLevelController,现在将看到来自控制器的TRACElog,而其他软件包中将不再有INFOlog:

1
2
3
4
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.配置回发

如果使用SpringBoot默认使用的Logback,则可以在src / test / resources中的logback-test.xml文件中设置日志级别:

1
2
3
4
5
6
7
8
9
10
11
12
13
<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>

上面的示例显示了如何在我们的Logback配置中设置日志级别以进行测试。 根日志级别设置为INFO,com.baeldung.testloglevel包的日志级别设置为DEBUG。

同样,让我们从上面应用设置后检查输出:

1
2
3
4
5
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。 基于配置文件的Logback配置

为测试设置特定于配置文件的配置的另一种方法是在我们的配置文件的application.properties中设置logging.configproperty:

1
logging.config=classpath:logback-testloglevel.xml

或者,如果要在类路径上使用单个Logback配置,则可以使用logback.xml中的springProfileelement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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调用TestLogLevelController,我们将获得以下输出:

1
2
3
4
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,则输出为:

1
2
3
4
5
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. Log4J的替代方案

或者,如果我们使用Log4J2,则可以在src / test / resources中的log4j2-spring.xml文件中设置日志级别:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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.configproperty来设置Log4J配置的路径:

1
logging.config=classpath:log4j-testloglevel.xml

最后,让我们在应用上述设置后检查输出:

1
2
3
4
5
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

六,结论

在本文中,我们学习了如何在测试Spring Boot应用程序时设置日志级别。 我们探索了许多不同的配置方式。

在Spring Boot的application.properties中设置日志级别显示为最简单,尤其是在使用@SpringBootTest批注时。

与往常一样,这些示例的源代码已在GitHub上结束。