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