001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014 015package ch.qos.logback.classic.servlet; 016 017import jakarta.servlet.ServletContextEvent; 018import jakarta.servlet.ServletContextListener; 019 020import org.slf4j.ILoggerFactory; 021import org.slf4j.LoggerFactory; 022 023import ch.qos.logback.classic.LoggerContext; 024import ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory; 025import ch.qos.logback.core.spi.ContextAwareBase; 026 027/** 028 * Allows for graceful shutdown of the {@link LoggerContext} associated with 029 * this web-app. 030 * 031 * @author Ceki Gulcu 032 * @since 1.1.10 033 */ 034public class LogbackServletContextListener implements ServletContextListener { 035 036 ContextAwareBase contextAwareBase = new ContextAwareBase(); 037 038 @Override 039 public void contextInitialized(ServletContextEvent sce) { 040 041 } 042 043 @Override 044 public void contextDestroyed(ServletContextEvent sce) { 045 046 ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory(); 047 if (iLoggerFactory instanceof LoggerContext) { 048 LoggerContext loggerContext = (LoggerContext) iLoggerFactory; 049 contextAwareBase.setContext(loggerContext); 050 StatusViaSLF4JLoggerFactory.addInfo("About to stop " + loggerContext.getClass().getCanonicalName() + " [" 051 + loggerContext.getName() + "]", this); 052 loggerContext.stop(); 053 } 054 } 055 056}