carlspring carlspring - 1 year ago 140
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...?

Answer Source

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() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download