I am trying to debug a spring boot application during tests especially see the log output.
I am not sure how to get the same autoconfigure log output during tests as the one I get when I run the application.
I have tried this (from src/main/resources/application-test.properties
):
logging.level.org.springframework.boot.autoconfigure.test=DEBUG
and
logging.level.org.springframework.boot.autoconfigure=DEBUG
By the way I use log4j with the following configuration (from src/main/resources/log4j.properties
):
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
edit: I have migrated to logback. Here is my src/main/resources/logback-test.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<logger name="org.springframework.boot.autoconfigure" level="debug"/>
<root level="warn">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
I still don't get any autoconfigure information during tests...
Now we simply need to run the application and hit http://localhost:8080/log to see the log messages.
Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available.
The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.
@SpringBootTest This annotation works by creating the ApplicationContext used in our tests through SpringApplication. It starts the embedded server, creates a web environment and then enables @Test methods to do integration testing. By default, @SpringBootTest does not start a server.
I don't believe it is good idea to combine explicit log4j configuration (log4j.properties) with Spring Boot one. I would use one or the other.
Auto-configuration information is printed when DEBUG level is configured for org.springframework.boot.autoconfigure.logging
package.
In this case log4j.properties seem to apply. Try to change:
log4j.rootLogger=DEBUG, stdout
Or if you decide go with application properties:
logging.level.org.springframework.boot.autoconfigure.logging=DEBUG
BTW, log4j is ancient technology. You should migrate to LogBack or log4j2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With