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 :

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?

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
HelpMetestermkyongfmpdmbLee Namkyu Recent comment authors
newest oldest most voted
HelpMe
Guest
HelpMe

Not working for me. I’ve tried everything. I’ve copied the file verbatim, put it in the location indicated, and renamed it to logback-test.xml. No matter what I do I’m still getting logback output to my console. I want it gone!

tester
Guest
tester

Hi, How can i use the property logback.access.enabled to set disable the logging ?

fmpdmb
Guest
fmpdmb

My understanding is that you should only be seeing the logback logging if you logback.xml file contains debug=”true”, which I would question why you have that set.

mkyong
Guest
mkyong

During unit test, it created a lot of noise and display many default configuration messages, lazy to set debug for package. The faster way is disabled it with an empty configuration file like above.

fmpdmb
Guest
fmpdmb

That really doesn’t make any sense and is not my experience. You will only see the logback debug loggging if you have that debug property set or are running with -Dlogback.debug=true. If possible, could you provide an example project that demonstrates what you’re seeing? I’d be curious to take a look.

Lee Namkyu
Guest
Lee Namkyu

That’s great comment !!