Application logs are very useful when trying to understand why an application did what it did.
This is also true when you debug a failing test. They are less useful while your tests are succeeding. Then they are just irritating and unnecessary clutter. Especially when they contain expected exceptions of those tests.
I recently got so fed up with these kinds of logs, that I wrote a small library for suppressing them. What it does is quite simple:
It stores all existing loggers and their configured appenders.
Before executing a test, it replaces all appenders with a
While the tests are executed, this appender remembers all logging events.
After the test was executed, it restores the original logger configuration.
Only if the test failed, the collected log events are sent to their corresponding original appenders.
The library is licensed under
Apache2.0 and available on Maven Central:
You can check out the source code on GitHub. Included in this library is a JUnit 4
Rule and a JUnit 5
Extension. Additionally it is assumed, that logging is done with Logback.