Main Tutorials

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 Author

author image
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

Subscribe
Notify of
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
tester
6 years ago

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

martin
1 year ago

uhm, IIUC you got all those info only because your configuration contains error. IIUC logback does not print these otherwise.
So the post is correct how to suppress logs in tests, but those info ones should be tackled by fixing your logback configuration.

tapiov
3 years ago

Thanks! This (the empty conf) finally help me to get rid of “DEBUG org.apache.commons.beanutils.” spam in my unit tests with Spring Boot. It was insanely hard to find help for this simple task; Spring boot has too many bells and whistles if you’re not a logging specialist. And just want to stop the spam. Arg. Thanks again, man.

Vladimir
3 years ago

Thanks, mkyong!
Your post is very useful.
The second solution suppressed all output to the console.

HelpMe
5 years ago

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!

fmpdmb
8 years ago

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
7 years ago
Reply to  fmpdmb

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
7 years ago
Reply to  mkyong

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
8 years ago

That’s great comment !!