1. Overview
1.概述
In this tutorial, we’ll learn how to set the log level when running tests for a Spring Boot application.
在本教程中,我们将学习如何在为Spring Boot应用程序运行测试时设置日志级别。
Although we can mostly ignore the logs while our tests are passing, choosing the right log level can be critical if we need to diagnose failed tests.
虽然我们可以在测试通过时忽略日志,但如果我们需要诊断失败的测试,选择正确的日志级别可能是至关重要的。
2. The Importance of the Log Level
2.日志级别的重要性
Configuring the log level correctly can save us a lot of time.
正确配置日志级别可以为我们节省很多时间。
For example, if tests are failing on a CI server, but passing on our development machine, we won’t be able to diagnose the failing tests unless we have enough log output. Conversely, if we log too much detail, it might be more difficult to find useful information.
例如,如果测试在CI服务器上失败,但在我们的开发机器上通过,我们将无法诊断失败的测试,除非我们有足够的日志输出。相反,如果我们记录了太多的细节,可能就更难找到有用的信息。
To achieve the right amount of detail, we can fine-tune the logging levels of our application’s packages. If we find that a Java package is more critical for our tests, we can give it a lower level, like DEBUG. Similarly, to avoid having too much noise in our logs, we can configure a higher level, say INFO or ERROR, for packages that are less important.
为了实现正确的细节,我们可以微调我们应用程序的包的日志级别。如果我们发现一个Java包对我们的测试比较关键,我们可以给它一个较低的级别,比如DEBUG。同样,为了避免在日志中出现太多噪音,我们可以为不太重要的包配置一个较高的级别,例如INFO或ERROR。
Let’s explore various ways of setting the logging level.
让我们探讨一下设置日志级别的各种方法。
3. Logging Settings in application.properties
3.application.properties中的日志设置
If we want to modify the log level in our tests, there’s a property we can set in src/test/resources/application.properties:
如果我们想在测试中修改日志级别,我们可以在src/test/resources/application.properties中设置一个属性。
logging.level.com.baeldung.testloglevel=DEBUG
This property will set the log level specifically for the com.baeldung.testloglevel package.
这个属性将设置日志级别,专门用于com.baeldung.testloglevel包。
Similarly, we can change the logging level for all packages by setting the root log level:
同样地,我们可以通过设置根日志级别来改变所有软件包的日志级别。
logging.level.root=INFO
Now let’s try out our logging settings by adding a REST endpoint that writes some logs:
现在,让我们通过添加一个写一些日志的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...";
}
}
As expected, if we call this endpoint in our tests, we’ll be able to see the DEBUG logs from TestLogLevelController:
正如预期的那样,如果我们在测试中调用这个端点,我们将能够从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
Setting the log level like this is quite easy, and we should definitely do it this way if our tests are annotated with @SpringBootTest. However, if we don’t use that annotation, we’ll have to configure the log level in a different way.
像这样设置日志级别是很容易的,如果我们的测试有@SpringBootTest的注释,我们肯定应该这样做。然而,如果我们不使用该注释,我们将不得不以不同的方式配置日志级别。
3.1. Profile-based Logging Settings
3.1.基于配置文件的日志设置
Although putting the settings into src/test/application.properties would work in most situations, there might be cases where we would like to have different settings for one test or a group of tests.
尽管将设置放入src/test/application.properties在大多数情况下都是可行的,但在某些情况下,我们可能希望对一个测试或一组测试拥有不同的设置。
In that case, we can add a Spring profile to our test by using the ActiveProfiles annotation:
在这种情况下,我们可以通过使用ActiveProfiles注解向我们的测试添加Spring配置文件。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
@ActiveProfiles("logging-test")
public class TestLogLevelWithProfileIntegrationTest {
// ...
}
Our logging settings will then be in a special application-logging-test.properties file within src/test/resources:
然后我们的日志设置将在src/test/resources中的一个特殊的application-logging-test.properties文件中。
logging.level.com.baeldung.testloglevel=TRACE
logging.level.root=ERROR
If we call TestLogLevelController from our tests with the described settings, we’ll now see the TRACE logs from our controller, and there will be no more INFO logs from other packages:
如果我们从我们的测试中调用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. Configuring Logback
4.配置日志回放
If we use Logback, which is used by default in Spring Boot, we can set the log level in the logback-test.xml file within src/test/resources:
如果我们使用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>
The above example shows how to set the log level in our Logback configuration for tests. The root log level is set to INFO and the log level for our com.baeldung.testloglevel package is set to DEBUG.
上面的例子显示了如何在我们的 Logback 配置中设置测试的日志级别。根日志级别被设置为INFO,我们的com.baeldung.testloglevel包的日志级别被设置为DEBUG。
Again, let’s check the output after applying the settings from above:
再次,让我们检查一下应用上述设置后的输出。
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-based Logback Configuration
4.1.基于配置文件的日志回传配置
Another way to set up a profile-specific configuration for our tests is to set the logging.config property in application.properties for our profile:
另一种为我们的测试设置特定配置文件配置的方法是在application.properties中为我们的配置文件设置logging.config属性。
logging.config=classpath:logback-testloglevel.xml
Or, if we want to have a single Logback configuration on our classpath, we can use the springProfile element in logback.xml:
或者,如果我们想在classpath上有一个单一的Logback配置,我们可以使用springProfile元素在logback.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>
<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>
Now if we call the TestLogLevelController in our tests with the profile logback-test1, we’ll get the following output:
现在,如果我们在测试中调用TestLogLevelController,配置文件为logback-test1,我们将得到以下输出。
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
Conversely, if we change the profile to logback-test2, the output will be:
相反,如果我们把配置文件改为logback-test2,输出将是。
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. A Log4J Alternative
5.一个Log4J的替代品
Alternatively, if we use Log4J2, we can set the log level in the log4j2-spring.xml file within src/test/resources:
另外,如果我们使用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>
We can set the path of our Log4J configuration by setting the logging.config property in application.properties:
我们可以通过在application.properties中设置logging.config属性来设置我们的Log4J配置的路径。
logging.config=classpath:log4j-testloglevel.xml
Finally, let’s check the output after applying the above settings:
最后,让我们检查一下应用上述设置后的输出情况。
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. Conclusion
6.结论
In this article, we learned how to set the log level when testing a Spring Boot application. Then we explored a number of different ways of configuring it.
在这篇文章中,我们学习了如何在测试Spring Boot应用程序时设置日志级别。然后我们探讨了一些不同的配置方法。
Setting the log level in Spring Boot’s application.properties is the easiest option, especially when we’re using the @SpringBootTest annotation.
在Spring Boot的application.properties中设置日志级别是最简单的选择,特别是当我们使用@SpringBootTest注解时。
As always, the source code for these examples is over on GitHub.
一如既往,这些例子的源代码都在GitHub上。