Java – how to set the spring logging level during testing?
See English answers > spring boot test ignores logging Level 6
I have application. In my project root directory properties:
logging.level.root=INFO
This can control logging when spring starts the application and can run at normal run time
However, whenever I run any junit4 test, I will be sent spam by the page output by debug, as shown below:
.... 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 these spam messages are almost impossible to see the actual relevant part How do I apply logging levels to test output?
I don't explicitly set any logging, and I use logback by default according to the document
Solution
From a general point of view, you can provide a separate logback - Test at the test resource level XML file In this file, you can add settings about the log level for the output you want, for example:
<?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>
I hope this will help you reduce the path of log output More information is recorded on logback's own page:
https://logback.qos.ch/manual/configuration.html
It mentions at the top:
