This repo contains code that demonstrates how to capture Logback logging output for unit tests.
Sometimes, unit testing involves verifying that code logs the right message at the correct logging level.
Unsurprisingly, this is easily achievable with a custom Logback appender in your unit tests.
The class CodeWithFancyLogging.java is built to log a "fancy" logging message with a given format. The LogbackCapturingAppender.java "injects" itself into the logger used by CodeWithFancyLogging.java
such that it can receive and capture logging events.
Corresponding unit tests in CodeWithFancyLoggingTest.java use this custom appender to capture logging output in flight and compare the resulting strings against what is expected.
Clone the repo.
git clone https://github.com/markkolich/logback-capturing-appender.git
cd logback-capturing-appender
From your favorite command line, invoke:
mvn clean test
You can also run the unit tests from within IntelliJ using the usual dance โ right click on the test
package and select "Run tests in...".
Copyright (c) 2015 Mark S. Kolich
All code in this project is freely available for use and redistribution under the MIT License.
See LICENSE for details.