View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.web;
18  
19  import java.util.EnumSet;
20  import java.util.Set;
21  import javax.servlet.DispatcherType;
22  import javax.servlet.FilterRegistration;
23  import javax.servlet.ServletContainerInitializer;
24  import javax.servlet.ServletContext;
25  import javax.servlet.ServletException;
26  
27  import org.apache.logging.log4j.Logger;
28  import org.apache.logging.log4j.status.StatusLogger;
29  
30  /**
31   * In a Servlet 3.0 or newer environment, this initializer is responsible for starting up Log4j logging before anything
32   * else happens in application initialization. For consistency across all containers, if the effective Servlet major
33   * version of the application is less than 3.0, this initializer does nothing.
34   */
35  public class Log4jServletContainerInitializer implements ServletContainerInitializer {
36  
37      private static final Logger LOGGER = StatusLogger.getLogger();
38  
39      @Override
40      public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
41          if (servletContext.getMajorVersion() > 2 && servletContext.getEffectiveMajorVersion() > 2 &&
42                  !"true".equalsIgnoreCase(servletContext.getInitParameter(
43                          Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED
44                  ))) {
45              LOGGER.debug("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");
46  
47              final FilterRegistration.Dynamic filter =
48                      servletContext.addFilter("log4jServletFilter", Log4jServletFilter.class);
49              if (filter == null) {
50                  LOGGER.warn("WARNING: In a Servlet 3.0+ application, you should not define a " +
51                      "log4jServletFilter in web.xml. Log4j 2 normally does this for you automatically. Log4j 2 " +
52                      "web auto-initialization has been canceled.");
53                  return;
54              }
55  
56              final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
57              initializer.start();
58              initializer.setLoggerContext(); // the application is just now starting to start up
59  
60              servletContext.addListener(new Log4jServletContextListener());
61  
62              filter.setAsyncSupported(true); // supporting async when the user isn't using async has no downsides
63              filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
64          }
65      }
66  }