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