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