How to intercept SLF4J (with logback) logging via a JUnit test?

Is it possible to somehow intercept the logging (SLF4J + logback) and get an InputStream (or something else that is readable) via a JUnit test case...?


  • You can create a custom appender

    public class TestAppender extends AppenderBase<LoggingEvent> {
        static List<LoggingEvent> events = new ArrayList<>();
        protected void append(LoggingEvent e) {

    and configure logback-test.xml to use it. Now we can check logging events from our test:

    public void test() {

    NOTE: Use ILoggingEvent if you do not get any output - see the comment section for the reasoning.