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 015 * this web-app. 016 * 017 * @author Ceki Gulcu 018 * @since 1.1.10 019 */ 020public class LogbackServletContextListener implements ServletContextListener { 021 022 ContextAwareBase contextAwareBase = new ContextAwareBase(); 023 024 @Override 025 public void contextInitialized(ServletContextEvent sce) { 026 027 } 028 029 @Override 030 public void contextDestroyed(ServletContextEvent sce) { 031 032 ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory(); 033 if (iLoggerFactory instanceof LoggerContext) { 034 LoggerContext loggerContext = (LoggerContext) iLoggerFactory; 035 contextAwareBase.setContext(loggerContext); 036 StatusViaSLF4JLoggerFactory.addInfo("About to stop " + loggerContext.getClass().getCanonicalName() + " [" 037 + loggerContext.getName() + "]", this); 038 loggerContext.stop(); 039 } 040 } 041 042}