11

My Spring Boot testing stack is Maven + Surefire + JUnit4. I am annotating the tests with @RunWith(SpringJUnit4ClassRunner.class).

I have application.properties in my project root with this line:

logging.level.root=INFO

This controls the logging when running the Spring boot app and it works on normal runs.

However, whenever I run any JUnit4 tests, I am spammed by pages of DEBUG output like this:

....
17:43:20.500 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'autoConfigurationReport'
17:43:20.500 [main] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registered bean definition for imported class 'org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration'
17:43:20.501 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.autoconfigure.condition.BeanTypeRegistry'
17:43:20.502 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'autoConfigurationReport'
....

All this spam makes it almost impossible to see the actually relevant parts. How can I apply the logging levels to test output?

I haven't set any logging explicitly, and according to the docs Logback is used by default.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
  • Do you have an `application.properties` in `/test/resources`? You can also pass arbitrary properties when executing the tests with `-Dspring.foo.bar=baz`. – jonrsharpe May 29 '17 at 07:57
  • @jonrsharpe I just tried copying my application.properties there but it didn't change anything. – Hubert Grzeskowiak May 29 '17 at 07:58

1 Answers1

18

From a general perspective, you can provide a seperate logback-test.xml-file at the test-resource level. In this file you can add settings regarding the log-level targeted at the output you'd like such as:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
    </layout>
  </appender>

  <logger name="com.your.package" level="DEBUG">
    <appender-ref ref="CONSOLE"/>
  </logger>

  <logger name="org.springframework" level="WARN">
    <appender-ref ref="CONSOLE"/>
  </logger>

  <logger name="org.hibernate" level="WARN">
    <appender-ref ref="CONSOLE"/>
  </logger>

  <logger name="org.eclipse" level="WARN">
    <appender-ref ref="CONSOLE"/>
  </logger>

  <logger name="jndi" level="WARN">
    <appender-ref ref="CONSOLE"/>
  </logger>

  <logger name="org.apache.http.wire" level="WARN">
      <appender-ref ref="CONSOLE"/>
  </logger>

  <root level="DEBUG">
      <appender-ref ref="CONSOLE"/>
  </root>

</configuration>

Hope this helps you somewhat on the path to decreasing the log output. More is documented at logback's own page:

https://logback.qos.ch/manual/configuration.html

Its mentioned in the top section:

Let us begin by discussing the initialization steps that logback follows to try to configure itself: 1.Logback tries to find a file called logback-test.xml in the classpath.

2.If no such file is found, logback tries to find a file called logback.groovy in the classpath.

3.If no such file is found, it checks for the file logback.xml in the classpath..

4.If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.

5.If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

vegaasen
  • 1,022
  • 7
  • 16
  • This helps a lot. Now I am only spammed by one page of Logback's output about setting logger levels. Looks like there is some other logger used before Logback is even set up. – Hubert Grzeskowiak May 29 '17 at 08:04
  • 1
    Without the appender definition and references it actually works perfectly! Found that out by trying out an answer by Michael Hegner here: https://stackoverflow.com/a/35254610/2445864 – Hubert Grzeskowiak May 29 '17 at 14:23
  • Nice! Removed my references as well in one of our applications, and it seems to remove all the overhead pointless output. Thanks! :) – vegaasen May 29 '17 at 14:49
  • If you want, you can edit your answer accordingly and I will accept it. – Hubert Grzeskowiak May 29 '17 at 15:07
  • Just noticed that if you omit the CONSOLE definition, the sole existence of the file causes all logging output to go away. That said, this is the true answer. – Hubert Grzeskowiak Jun 01 '17 at 15:11