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