001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.web;
018    
019    import javax.servlet.ServletContext;
020    import javax.servlet.ServletContextEvent;
021    import javax.servlet.ServletContextListener;
022    
023    import org.apache.logging.log4j.core.LoggerContext;
024    import org.apache.logging.log4j.core.config.Configurator;
025    import org.apache.logging.log4j.core.lookup.Interpolator;
026    import org.apache.logging.log4j.core.lookup.StrLookup;
027    import org.apache.logging.log4j.core.lookup.StrSubstitutor;
028    
029    /**
030     * Saves the LoggerContext into the ServletContext as an attribute.
031     */
032    public class Log4jContextListener implements ServletContextListener {
033    
034        /**
035         * The name of the attribute to use to store the LoggerContext into the ServletContext.
036         */
037        public static final String LOG4J_CONTEXT_ATTRIBUTE = "Log4jContext";
038    
039        /**
040         * The location of the configuration.
041         */
042        public static final String LOG4J_CONFIG = "log4jConfiguration";
043    
044        /**
045         * The name of the LoggerContext.
046         */
047        public static final String LOG4J_CONTEXT_NAME = "log4jContextName";
048    
049        private final StrSubstitutor subst = new StrSubstitutor(new Interpolator());
050    
051        /**
052         * Initialize Logging for the web application.
053         * @param event The ServletContextEvent.
054         */
055        @Override
056        public void contextInitialized(final ServletContextEvent event) {
057            final ServletContext context = event.getServletContext();
058            final String locn = subst.replace(context.getInitParameter(LOG4J_CONFIG));
059            String name = subst.replace(context.getInitParameter(LOG4J_CONTEXT_NAME));
060            if (name == null) {
061                name = context.getServletContextName();
062            }
063            if (name == null && locn == null) {
064                context.log("No Log4j context configuration provided");
065                return;
066            }
067            context.setAttribute(LOG4J_CONTEXT_ATTRIBUTE, Configurator.initialize(name, getClassLoader(context), locn));
068        }
069    
070        /**
071         * Shutdown logging for the web application.
072         * @param event The ServletContextEvent.
073         */
074        @Override
075        public void contextDestroyed(final ServletContextEvent event) {
076            final LoggerContext ctx = (LoggerContext) event.getServletContext().getAttribute(LOG4J_CONTEXT_ATTRIBUTE);
077            Configurator.shutdown(ctx);
078        }
079    
080        private ClassLoader getClassLoader(final ServletContext context) {
081            try {
082                // if container is Servlet 3.0, use its getClassLoader method
083                return (ClassLoader)context.getClass().getMethod("getClassLoader").invoke(context);
084            } catch (Exception ignore) {
085                // otherwise, use this class's class loader
086                return Log4jContextListener.class.getClassLoader();
087            }
088        }
089    }