Logback Disable logging in Unit Test

摘要: While the unit test is running in the IDE, the Logback is showing a lot of configuration or status like this :

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?

上一篇: Java 8 Filter a Map examples
下一篇: Java 8 Streams filter examples
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号