Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view autoconfigure log output during spring boot tests (integration tests)

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

like image 742
balteo Avatar asked Sep 26 '15 19:09

balteo


People also ask

How do I check spring boot logs?

Now we simply need to run the application and hit http://localhost:8080/log to see the log messages.

Can you control logging with spring boot How?

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.

What does the SpringBootTest annotation do?

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.

How does SpringBootTest work?

@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.


1 Answers

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.

like image 155
luboskrnac Avatar answered Oct 20 '22 02:10

luboskrnac