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