Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Spring logging level while testing? [duplicate]

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.

like image 621
Hubert Grzeskowiak Avatar asked May 29 '17 07:05

Hubert Grzeskowiak


People also ask

How do you change the logging level in spring?

Spring boot logging levels To set the logging level for any logger, add properties starting with logging. level . Logging level can be one of one of TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF . The root logger can be configured using logging.

How do you set the log level to debug in junit tests?

The logging code searches for the log4j. properties file using the classpath. So all you need to do is ensure that your test log4j. properties file is in a location that it will find before the release file.

What is the default logging level in spring boot?

By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a logging. file or logging. path property (for example, in your application.

How does spring Boot define logging level?

Configuring Log Levels in Spring Boot The format to set the log level configuration is logging. level. [classpath] = [level] . The classpath is specified since different components of the application can be configured with different log levels, which is especially useful for code isolation and debugging.


1 Answers

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.

like image 143
vegaasen Avatar answered Sep 30 '22 18:09

vegaasen