How to set the log level when testing spring boot

Time:2020-1-11

1. overview

In this tutorial, I’ll show you how to set the spring boot log level during testing. Although we can ignore the log when the test passes, it is very important to choose the correct log level if we need to diagnose the failed test.

2. Importance of log level

Setting the log level correctly can save us a lot of time.

For example, if the test fails on the CI server, but passes on the development server. We will not be able to diagnose failed tests unless there is sufficient log output.

In order to get the correct amount of details, we can fine tune the application’s log level. If we find that a java package is more important to our test, we can give it a lower log level, such as debug. Similarly, in order to avoid too much interference in the log, we can configure a higher level of log level, such as info or error, for the less important packages.

Let’s explore various ways to set the log level!

3. Log settings in application.properties

If you want to modify the log level in the test, you can set the properties in Src / test / resources / application.properties:


logging.level.com.baeldung.testloglevel=DEBUG

This property sets the log level for the specified package com.baeldung.testloglevel.

Similarly, we can change the log level of all packages by setting the root log level


logging.level.root=INFO

Now try to set the log by adding a rest endpoint to write to the log.


@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 the test, we will be able to see the debug log from TestLogLevelController.


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

This is very simple to set the log level. If the test is annotated with @ springboottest, then we should definitely do this. However, if you do not use this annotation, you must configure the log level in another way.

3.1 profile based log settings

Although it is easy to use the configuration in SRC \ test \ application.properties in most scenarios, in some cases, we may want to set different configurations for one or a group of tests.

In this case, we can use the @ activeprofiles annotation to add a spring profile to the test:


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
@ActiveProfiles("logging-test")
public class TestLogLevelWithProfileIntegrationTest {
// ...
}

Log settings will be stored in application logging test.properties under Src / test / Resources Directory:


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

If you call testloglevelccontroller with the described settings, you will see the trace level logs printed in the controller, and you will not see other packages with more than info level logs.


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. Configure logback

If you use spring boot’s default logback, you can set the log level in the logback-text.xml file under the Src / test / Resources Directory:


<configuration>
 <include resource="/org/springframework/boot/logging/logback/base.xml"/>
 <appender name="STDOUT">
  <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 configure the log level for logback in the test.

The root log level is set to info, and the log level of the com.baeldung.testloglevel package is set to debug.

Once again, take a look at the log output after submitting the above configuration


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 configure logback based on profile

Another way to configure and specify the profile file is to set the logging.config property in the application.properties file:


logging.config=classpath:logback-testloglevel.xml

Or, if you want to have only one logback configuration in the classpath, you can use the springprofile property in logbacl.xml.


<configuration>
 <include resource="/org/springframework/boot/logging/logback/base.xml"/>
 <appender name="STDOUT">
  <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 call testloglevelcontroller with logback test1 configuration file, and you will get the following output:


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

On the other hand, if you change the configuration to logback-test2, the output will be as follows:


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. Optional log4j

In addition, if we use log4j2, we can configure the log level in the log4j2-spring.xml file under the SRC \ main \ resources directory.


<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 log4j configuration through the logging.config property in application.properties.


logging.config=classpath:log4j-testloglevel.xml

Finally, look at the output with the above configuration:


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

In this article, we learned how to set the logging level when testing an application with spring boot, and explored many different configuration methods. Using application.properties to set the log level in a spring boot application is the easiest, especially when we use the @ springboottest annotation.

As always, the source code for these samples is on GitHub.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.