All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.neuland.assertj.logging.Log4jLogEventCaptureAppender Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package de.neuland.assertj.logging;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.appender.AbstractAppender;

import java.util.ArrayList;
import java.util.List;


public class Log4jLogEventCaptureAppender extends AbstractAppender implements LogEventCaptureAppender {
    private final List logEvents = new ArrayList<>();

    public Log4jLogEventCaptureAppender() {
        super(Log4jLogEventCaptureAppender.class.getName(),
              null,
              null,
              false,
              null);
    }

    @Override
    public List getLogEvents() {
        return logEvents;
    }

    @Override
    public boolean isStarted() {
        return true;
    }

    @Override
    public void append(org.apache.logging.log4j.core.LogEvent event) {
        logEvents.add(new LogEvent(level(event),
                                   message(event),
                                   throwable(event)));
    }

    private LogLevel level(org.apache.logging.log4j.core.LogEvent event) {
        Level level = event.getLevel();
        if (Level.ERROR == level) {
            return LogLevel.ERROR;
        }
        if (Level.WARN == level) {
            return LogLevel.WARNING;
        }
        if (Level.INFO == level) {
            return LogLevel.INFO;
        }
        return LogLevel.IGNORED;
    }

    private String message(org.apache.logging.log4j.core.LogEvent event) {
        if (event.getMessage() == null) {
            return null;
        }
        return event.getMessage().getFormattedMessage();
    }

    private Throwable throwable(org.apache.logging.log4j.core.LogEvent event) {
        return event.getThrown();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy