Logback Disable logging in Unit Test
By:Roy.LiuLast updated:2019-08-17
While the unit test is running in the IDE, the Logback is showing a lot of configuration or status like this :
21:16:59,569 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 21:16:59,569 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 21:16:59,569 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at ... //... omitted for readability. 21:17:00,051 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ch.qos.logback] 21:17:00,051 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to OFF 21:17:00,051 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 21:17:00,051 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 21:17:00,053 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@20e2cbe0 - Registering current configuration as safe fallback point java.lang.AssertionError: Expected: is <3> but: was <2>at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
It is really annoying, especially for the failed test, because I need to scroll down manually for the error message!
1. Solution – Empty Configuration
To fix it, create an empty configuration file as logback-test.xml, and save it under $project/src/test/resources
$project/src/test/resources/logback-test.xml
<!-- only one line, shut up logback ! --> <configuration />
Run the unit test again, no more nonsense, silence is golden.
2. Solution – NopStatusListener
Alternatively, add a NopStatusListener like this :
logback-test.xml
<configuration> <statusListener class="ch.qos.logback.core.status.NopStatusListener" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <root level="error"> <appender-ref ref="STDOUT"/> </root> </configuration>
Check this – How to stop logback status INFO at the start of every log?
From:一号门
Previous:Java 8 Filter a Map examples
COMMENTS