001package ch.qos.logback.classic.servlet;
002
003import javax.servlet.ServletContextEvent;
004import javax.servlet.ServletContextListener;
005
006import org.slf4j.ILoggerFactory;
007import org.slf4j.LoggerFactory;
008
009import ch.qos.logback.classic.LoggerContext;
010import ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory;
011import ch.qos.logback.core.spi.ContextAwareBase;
012
013/**
014 * Allows for graceful shutdown of the {@link LoggerContext} associated with this web-app.
015 * 
016 * @author Ceki Gulcu
017 * @since 1.1.10
018 */
019public class LogbackServletContextListener implements ServletContextListener {
020
021    ContextAwareBase contextAwareBase = new ContextAwareBase();
022
023    @Override
024    public void contextInitialized(ServletContextEvent sce) {
025
026    }
027
028    @Override
029    public void contextDestroyed(ServletContextEvent sce) {
030
031        ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
032        if (iLoggerFactory instanceof LoggerContext) {
033            LoggerContext loggerContext = (LoggerContext) iLoggerFactory;
034            contextAwareBase.setContext(loggerContext);
035            StatusViaSLF4JLoggerFactory.addInfo("About to stop " + loggerContext.getClass().getCanonicalName() + " [" + loggerContext.getName() + "]", this);
036            loggerContext.stop();
037        }
038    }
039
040}