Is it possible to find logback log files programmatically?

Alexey Romanov picture Alexey Romanov · Aug 15, 2011 · Viewed 12.6k times · Source

It would be useful to automatically attach log files to support emails. I could set the path programmatically (as in Setting Logback Appender path programmatically), but I'd prefer to let users configure logging in the familiar way via logback.xml. So, can I find the files logback uses for logging?

Answer

tafoo85 picture tafoo85 · Aug 24, 2011

You can get the list of all appenders in a certain context. To do this:

LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
for (Logger logger : context.getLoggerList()) {
        for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
            Appender<ILoggingEvent> appender = index.next();
        }
    }

This iterates over the list of all appenders in all loggers for the current context.