How to set Spring logging level while testing?

Hubert Grzeskowiak :

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.

vegaasen :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=444796&siteId=1