carlspring carlspring - 2 months ago 25
Java Question

How to intercept SLF4J logging via a JUnit test?

Is it possible to somehow intercept the logging (SLF4J + logback) and get an

(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() {