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 org.apache.logging.log4j.core.LoggerContext;
20  import org.apache.logging.log4j.core.config.Configurator;
21  
22  import javax.servlet.ServletContext;
23  import javax.servlet.ServletContextEvent;
24  import javax.servlet.ServletContextListener;
25  
26  /**
27   * Saves the LoggerContext into the ServletContext as an attribute.
28   */
29  public class Log4jContextListener implements ServletContextListener {
30  
31      /**
32       * The name of the attribute to use to store the LoggerContext into the ServletContext.
33       */
34      public static final String LOG4J_CONTEXT_ATTRIBUTE = "Log4JContext";
35  
36      /**
37       * The location of the configuration.
38       */
39      public static final String LOG4J_CONFIG = "log4jConfiguration";
40  
41      /**
42       * The name of the LoggerContext.
43       */
44      public static final String LOG4J_CONTEXT_NAME = "log4jContextName";
45  
46      /**
47       * Initialize Logging for the web application.
48       * @param event The ServletContextEvent.
49       */
50      public void contextInitialized(final ServletContextEvent event) {
51          final ServletContext context = event.getServletContext();
52          final String locn = context.getInitParameter(LOG4J_CONFIG);
53          String name = context.getInitParameter(LOG4J_CONTEXT_NAME);
54          if (name == null) {
55              name = context.getServletContextName();
56          }
57          if (name == null && locn == null) {
58              context.log("No Log4j context configuration provided");
59              return;
60          }
61          context.setAttribute(LOG4J_CONTEXT_ATTRIBUTE, Configurator.initialize(name, getClassLoader(context), locn));
62      }
63  
64      /**
65       * Shutdown logging for the web application.
66       * @param event The ServletContextEvent.
67       */
68      public void contextDestroyed(final ServletContextEvent event) {
69          final LoggerContext ctx = (LoggerContext) event.getServletContext().getAttribute(LOG4J_CONTEXT_ATTRIBUTE);
70          Configurator.shutdown(ctx);
71      }
72  
73      private ClassLoader getClassLoader(final ServletContext context) {
74          try {
75              // if container is Servlet 3.0, use its getClassLoader method
76              return (ClassLoader)context.getClass().getMethod("getClassLoader").invoke(context);
77          } catch (Exception ignore) {
78              // otherwise, use this class's class loader
79              return Log4jContextListener.class.getClassLoader();
80          }
81      }
82  }