View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.web;
18  
19  import javax.servlet.ServletContext;
20  import javax.servlet.ServletContextEvent;
21  import javax.servlet.ServletContextListener;
22  
23  import org.apache.logging.log4j.core.LoggerContext;
24  import org.apache.logging.log4j.core.config.Configurator;
25  import org.apache.logging.log4j.core.lookup.Interpolator;
26  import org.apache.logging.log4j.core.lookup.StrLookup;
27  import org.apache.logging.log4j.core.lookup.StrSubstitutor;
28  
29  /**
30   * Saves the LoggerContext into the ServletContext as an attribute.
31   */
32  public class Log4jContextListener implements ServletContextListener {
33  
34      /**
35       * The name of the attribute to use to store the LoggerContext into the ServletContext.
36       */
37      public static final String LOG4J_CONTEXT_ATTRIBUTE = "Log4jContext";
38  
39      /**
40       * The location of the configuration.
41       */
42      public static final String LOG4J_CONFIG = "log4jConfiguration";
43  
44      /**
45       * The name of the LoggerContext.
46       */
47      public static final String LOG4J_CONTEXT_NAME = "log4jContextName";
48  
49      private final StrSubstitutor subst = new StrSubstitutor(new Interpolator());
50  
51      /**
52       * Initialize Logging for the web application.
53       * @param event The ServletContextEvent.
54       */
55      @Override
56      public void contextInitialized(final ServletContextEvent event) {
57          final ServletContext context = event.getServletContext();
58          final String locn = subst.replace(context.getInitParameter(LOG4J_CONFIG));
59          String name = subst.replace(context.getInitParameter(LOG4J_CONTEXT_NAME));
60          if (name == null) {
61              name = context.getServletContextName();
62          }
63          if (name == null && locn == null) {
64              context.log("No Log4j context configuration provided");
65              return;
66          }
67          context.setAttribute(LOG4J_CONTEXT_ATTRIBUTE, Configurator.initialize(name, getClassLoader(context), locn));
68      }
69  
70      /**
71       * Shutdown logging for the web application.
72       * @param event The ServletContextEvent.
73       */
74      @Override
75      public void contextDestroyed(final ServletContextEvent event) {
76          final LoggerContext ctx = (LoggerContext) event.getServletContext().getAttribute(LOG4J_CONTEXT_ATTRIBUTE);
77          Configurator.shutdown(ctx);
78      }
79  
80      private ClassLoader getClassLoader(final ServletContext context) {
81          try {
82              // if container is Servlet 3.0, use its getClassLoader method
83              return (ClassLoader)context.getClass().getMethod("getClassLoader").invoke(context);
84          } catch (Exception ignore) {
85              // otherwise, use this class's class loader
86              return Log4jContextListener.class.getClassLoader();
87          }
88      }
89  }