1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.servlet;
16  
17  import static ch.qos.logback.core.CoreConstants.DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY;
18  
19  import java.util.Set;
20  
21  import jakarta.servlet.ServletContainerInitializer;
22  import jakarta.servlet.ServletContext;
23  import jakarta.servlet.ServletException;
24  
25  import ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory;
26  import ch.qos.logback.core.util.OptionHelper;
27  
28  /**
29   * Attaches a new instance of {@link LogbackServletContextListener} to the
30   * current web-applications {@link ServletContext}.
31   * 
32   * @author Ceki Gulcu
33   * @since 1.1.10
34   */
35  public class LogbackServletContainerInitializer implements ServletContainerInitializer {
36  
37      @Override
38      public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
39  
40          if (isDisabledByConfiguration(ctx)) {
41              StatusViaSLF4JLoggerFactory.addInfo("Due to deployment instructions will NOT register an instance of "
42                      + LogbackServletContextListener.class + " to the current web-app", this);
43  
44              return;
45          }
46  
47          StatusViaSLF4JLoggerFactory.addInfo(
48                  "Adding an instance of  " + LogbackServletContextListener.class + " to the current web-app", this);
49          LogbackServletContextListener lscl = new LogbackServletContextListener();
50          ctx.addListener(lscl);
51      }
52  
53      /**
54       * Search for value of DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY in the web-app
55       * first, then as a system property and as an environment variable last.
56       *
57       * @param ctx
58       * @return True if value of DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY is
59       *         available and set to "true", false otherwise.
60       */
61      boolean isDisabledByConfiguration(ServletContext ctx) {
62          String disableAttributeStr = null;
63          Object disableAttribute = ctx.getInitParameter(DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY);
64          if (disableAttribute instanceof String) {
65              disableAttributeStr = (String) disableAttribute;
66          }
67  
68          if (OptionHelper.isNullOrEmptyOrAllSpaces(disableAttributeStr)) {
69              disableAttributeStr = OptionHelper.getSystemProperty(DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY);
70          }
71  
72          if (OptionHelper.isNullOrEmptyOrAllSpaces(disableAttributeStr)) {
73              disableAttributeStr = OptionHelper.getEnv(DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY);
74          }
75  
76          return Boolean.parseBoolean(disableAttributeStr);
77      }
78  
79  }