27. March 2017
1 min

Suppressing Logs in Successful Tests

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:

  1. It stores all existing loggers and their configured appenders.
  2. Before executing a test, it replaces all appenders with a RecordingAppender.
  3. While the tests are executed, this appender remembers all logging events.
  4. After the test was executed, it restores the original logger configuration.
  5. Only if the test failed, the collected log events are sent to their corresponding original appenders.

The library is licensed under Apache 2.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.


Class Under Test

In JUnit 5

In JUnit 4

Comment article