How to Get All Appenders in Logback Context in Java


Let’s see how we can obtain the names of all Logback appenders in the current LoggerContext.

We can first obtain the LoggerContext with LoggerFactory::getILoggerFactory.

Each logger in this context (i.e. LoggerContext::getLoggerList) will contain an iterator for its appenders.

We can access each appender and operate on each one using a simple nested loop.

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
for (Logger logger : context.getLoggerList()) {
  Iterator<Appender<ILoggingEvent>> iter = logger.iteratorForAppenders();
  while (iter.hasNext()) {
    Appender<ILoggingEvent> appender = iter.next();
    // Do something with `appender`
  }
}