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.io.IOException;
20  
21  import javax.servlet.Filter;
22  import javax.servlet.FilterChain;
23  import javax.servlet.FilterConfig;
24  import javax.servlet.ServletContext;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  
29  /**
30   * This is responsible for the following:
31   * <ul>
32   *     <li>Clearing the logger context when the application has finished starting up.</li>
33   *     <li>Setting the logger context before processing a request and clearing it after processing a request.</li>
34   *     <li>Setting the logger context when the application is starting to shut down.</li>
35   * </ul>
36   * This filter is a once-per-request filter. It is capable of filtering all the different types of requests
37   * (standard, asynchronous, error, etc.) but will not apply processing if the filter matches multiple times on the same
38   * logical request.
39   */
40  public class Log4jServletFilter implements Filter {
41  
42      static final String ALREADY_FILTERED_ATTRIBUTE = Log4jServletFilter.class.getName() + ".FILTERED";
43  
44      private ServletContext servletContext;
45      private Log4jWebLifeCycle initializer;
46  
47      @Override
48      public void init(final FilterConfig filterConfig) throws ServletException {
49          this.servletContext = filterConfig.getServletContext();
50          this.servletContext.log("Log4jServletFilter initialized.");
51  
52          this.initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(this.servletContext);
53          this.initializer.clearLoggerContext(); // the application is mostly finished starting up now
54      }
55  
56      @Override
57      public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
58              throws IOException, ServletException {
59          if (request.getAttribute(ALREADY_FILTERED_ATTRIBUTE) != null) {
60              chain.doFilter(request, response);
61          } else {
62              request.setAttribute(ALREADY_FILTERED_ATTRIBUTE, true);
63  
64              try {
65                  this.initializer.setLoggerContext();
66  
67                  chain.doFilter(request, response);
68              } finally {
69                  this.initializer.clearLoggerContext();
70              }
71          }
72      }
73  
74      @Override
75      public void destroy() {
76          if (this.servletContext == null || this.initializer == null) {
77              throw new IllegalStateException("Filter destroyed before it was initialized.");
78          }
79          this.servletContext.log("Log4jServletFilter destroyed.");
80  
81          this.initializer.setLoggerContext(); // the application is just now starting to shut down
82      }
83  }