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.core.web;
018
019import java.util.EnumSet;
020import java.util.Set;
021import javax.servlet.DispatcherType;
022import javax.servlet.FilterRegistration;
023import javax.servlet.ServletContainerInitializer;
024import javax.servlet.ServletContext;
025import javax.servlet.ServletException;
026
027/**
028 * In a Servlet 3.0 or newer environment, this initializer is responsible for starting up Log4j logging before anything
029 * else happens in application initialization. For consistency across all containers, if the effective Servlet major
030 * version of the application is less than 3.0, this initializer does nothing.
031 */
032public class Log4jServletContainerInitializer implements ServletContainerInitializer {
033
034    @Override
035    public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
036        if (servletContext.getMajorVersion() > 2 && servletContext.getEffectiveMajorVersion() > 2 &&
037                !"true".equalsIgnoreCase(servletContext.getInitParameter(
038                        Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED
039                ))) {
040            servletContext.log("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");
041
042            final FilterRegistration.Dynamic filter =
043                    servletContext.addFilter("log4jServletFilter", Log4jServletFilter.class);
044            if (filter == null) {
045                servletContext.log("WARNING: In a Servlet 3.0+ application, you should not define a " +
046                        "log4jServletFilter in web.xml. Log4j 2 normally does this for you automatically. Log4j 2 " +
047                        "web auto-initialization has been canceled.");
048                return;
049            }
050
051            final Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext);
052            initializer.initialize();
053            initializer.setLoggerContext(); // the application is just now starting to start up
054
055            servletContext.addListener(new Log4jServletContextListener());
056
057            filter.setAsyncSupported(true); // supporting async when the user isn't using async has no downsides
058            filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
059        }
060    }
061}