Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class LocalAppender extends AppenderBase<ILoggingEvent> {
private List<ILoggingEvent> events = new ArrayList<>();
public LocalAppender() {
start();
}
public static LocalAppender initialize(String... loggers) {
LocalAppender localAppender = new LocalAppender();
for (String loggerName : loggers) {
localAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory());
Logger logger = (Logger) LoggerFactory.getLogger(loggerName);
logger.addAppender(localAppender);
}
return localAppender;
}
public void cleanup() {
this.stop();
this.clearEvents();
}
@Override
public void append(ILoggingEvent e) {
events.add(e);
}
public List<ILoggingEvent> getEvents() {
return events;
}
public void clearEvents() {
events.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment