de.neuland.assertj.logging.Log4j12LogEventCaptureAppender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assertj-logging-log4j12 Show documentation
Show all versions of assertj-logging-log4j12 Show documentation
Logging Assertions for JUnit 4 and AssertJ
package de.neuland.assertj.logging;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import java.util.ArrayList;
import java.util.List;
public class Log4j12LogEventCaptureAppender extends AppenderSkeleton implements LogEventCaptureAppender {
private final List logEvents = new ArrayList<>();
@Override
public List getLogEvents() {
return logEvents;
}
@Override
public void append(LoggingEvent event) {
logEvents.add(new LogEvent(logLevel(event),
message(event),
throwable(event)));
}
@Override
public void close() {
}
@Override
public boolean requiresLayout() {
return false;
}
private LogLevel logLevel(LoggingEvent event) {
Level level = event.getLevel();
if (Level.ERROR.equals(level)) {
return LogLevel.ERROR;
}
if (Level.WARN.equals(level)) {
return LogLevel.WARNING;
}
if (Level.INFO.equals(level)) {
return LogLevel.INFO;
}
return LogLevel.IGNORED;
}
private String message(LoggingEvent event) {
return event.getRenderedMessage();
}
private Throwable throwable(LoggingEvent event) {
return event.getThrowableInformation() != null
? event.getThrowableInformation().getThrowable()
: null;
}
}