View Javadoc
1   package ch.qos.logback.classic.servlet;
2   
3   import javax.servlet.ServletContextEvent;
4   import javax.servlet.ServletContextListener;
5   
6   import org.slf4j.ILoggerFactory;
7   import org.slf4j.LoggerFactory;
8   
9   import ch.qos.logback.classic.LoggerContext;
10  import ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory;
11  import ch.qos.logback.core.spi.ContextAwareBase;
12  
13  /**
14   * Allows for graceful shutdown of the {@link LoggerContext} associated with this web-app.
15   * 
16   * @author Ceki Gulcu
17   * @since 1.1.10
18   */
19  public class LogbackServletContextListener implements ServletContextListener {
20  
21      ContextAwareBase contextAwareBase = new ContextAwareBase();
22  
23      @Override
24      public void contextInitialized(ServletContextEvent sce) {
25  
26      }
27  
28      @Override
29      public void contextDestroyed(ServletContextEvent sce) {
30  
31          ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
32          if (iLoggerFactory instanceof LoggerContext) {
33              LoggerContext loggerContext = (LoggerContext) iLoggerFactory;
34              contextAwareBase.setContext(loggerContext);
35              StatusViaSLF4JLoggerFactory.addInfo("About to stop " + loggerContext.getClass().getCanonicalName() + " [" + loggerContext.getName() + "]", this);
36              loggerContext.stop();
37          }
38      }
39  
40  }