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  
22  import javax.servlet.DispatcherType;
23  import javax.servlet.FilterRegistration;
24  import javax.servlet.ServletContainerInitializer;
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletException;
27  
28  /**
29   * In a Servlet 3.0 or newer environment, this initializer is responsible for starting up Log4j logging before anything
30   * else happens in application initialization. For consistency across all containers, if the effective Servlet major
31   * version of the application is less than 3.0, this initializer does nothing.
32   */
33  public class Log4jServletContainerInitializer implements ServletContainerInitializer {
34  
35      @Override
36      public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
37          if (servletContext.getMajorVersion() > 2 && servletContext.getEffectiveMajorVersion() > 2 &&
38                  !"true".equalsIgnoreCase(servletContext.getInitParameter(
39                          Log4jWebSupport.IS_LOG4J_AUTO_INITIALIZATION_DISABLED
40                  ))) {
41              servletContext.log("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment.");
42  
43              final FilterRegistration.Dynamic filter =
44                      servletContext.addFilter("log4jServletFilter", Log4jServletFilter.class);
45              if (filter == null) {
46                  servletContext.log("WARNING: In a Servlet 3.0+ application, you should not define a " +
47                          "log4jServletFilter in web.xml. Log4j 2 normally does this for you automatically. Log4j 2 " +
48                          "web auto-initialization has been canceled.");
49                  return;
50              }
51  
52              final Log4jWebLifeCycle initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext);
53              initializer.start();
54              initializer.setLoggerContext(); // the application is just now starting to start up
55  
56              servletContext.addListener(new Log4jServletContextListener());
57  
58              filter.setAsyncSupported(true); // supporting async when the user isn't using async has no downsides
59              filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
60          }
61      }
62  }