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 */
017package org.apache.logging.log4j.web;
018
019import java.util.Locale;
020import java.util.concurrent.TimeUnit;
021
022import javax.servlet.ServletContext;
023import javax.servlet.ServletContextEvent;
024import javax.servlet.ServletContextListener;
025
026import org.apache.logging.log4j.Logger;
027import org.apache.logging.log4j.core.LifeCycle2;
028import org.apache.logging.log4j.status.StatusLogger;
029import org.apache.logging.log4j.util.Strings;
030
031/**
032 * In environments older than Servlet 3.0, this initializer is responsible for starting up Log4j logging before anything
033 * else happens in application initialization. In all environments, this shuts down Log4j after the application shuts
034 * down.
035 */
036public class Log4jServletContextListener implements ServletContextListener {
037
038        private static final int DEFAULT_STOP_TIMEOUT = 30;
039    private static final TimeUnit DEFAULT_STOP_TIMEOUT_TIMEUNIT = TimeUnit.SECONDS;
040
041        private static final String KEY_STOP_TIMEOUT = "log4j.stop.timeout";
042        private static final String KEY_STOP_TIMEOUT_TIMEUNIT = "log4j.stop.timeout.timeunit";
043
044        private static final Logger LOGGER = StatusLogger.getLogger();
045
046    private ServletContext servletContext;
047    private Log4jWebLifeCycle initializer;
048
049    @Override
050    public void contextInitialized(final ServletContextEvent event) {
051        this.servletContext = event.getServletContext();
052        LOGGER.debug("Log4jServletContextListener ensuring that Log4j starts up properly.");
053
054        this.initializer = WebLoggerContextUtils.getWebLifeCycle(this.servletContext);
055        try {
056            this.initializer.start();
057            this.initializer.setLoggerContext(); // the application is just now starting to start up
058        } catch (final IllegalStateException e) {
059            throw new IllegalStateException("Failed to initialize Log4j properly.", e);
060        }
061    }
062
063    @Override
064        public void contextDestroyed(final ServletContextEvent event) {
065                if (this.servletContext == null || this.initializer == null) {
066                        LOGGER.warn("Context destroyed before it was initialized.");
067                        return;
068                }
069                LOGGER.debug("Log4jServletContextListener ensuring that Log4j shuts down properly.");
070
071                this.initializer.clearLoggerContext(); // the application is finished
072                // shutting down now
073                if (initializer instanceof LifeCycle2) {
074                        final String stopTimeoutStr = servletContext.getInitParameter(KEY_STOP_TIMEOUT);
075                        final long stopTimeout = Strings.isEmpty(stopTimeoutStr) ? DEFAULT_STOP_TIMEOUT
076                                        : Long.parseLong(stopTimeoutStr);
077                        final String timeoutTimeUnitStr = servletContext.getInitParameter(KEY_STOP_TIMEOUT_TIMEUNIT);
078                        final TimeUnit timeoutTimeUnit = Strings.isEmpty(timeoutTimeUnitStr) ? DEFAULT_STOP_TIMEOUT_TIMEUNIT
079                                        : TimeUnit.valueOf(timeoutTimeUnitStr.toUpperCase(Locale.ROOT));
080                        ((LifeCycle2) this.initializer).stop(stopTimeout, timeoutTimeUnit);
081                } else {
082                        this.initializer.stop();
083                }
084        }
085}