View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.helpers;
15  
16  import java.io.IOException;
17  
18  import javax.servlet.Filter;
19  import javax.servlet.FilterChain;
20  import javax.servlet.FilterConfig;
21  import javax.servlet.ServletException;
22  import javax.servlet.ServletRequest;
23  import javax.servlet.ServletResponse;
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.slf4j.MDC;
27  
28  import ch.qos.logback.classic.ClassicConstants;
29  
30  /**
31   * A servlet filter that inserts various values retrieved from the incoming http
32   * request into the MDC.
33   * <p/>
34   * <p/>
35   * The values are removed after the request is processed.
36   *
37   * @author Ceki G&uuml;lc&uuml;
38   */
39  public class MDCInsertingServletFilter implements Filter {
40  
41      public void destroy() {
42          // do nothing
43      }
44  
45      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
46  
47          insertIntoMDC(request);
48          try {
49              chain.doFilter(request, response);
50          } finally {
51              clearMDC();
52          }
53      }
54  
55      void insertIntoMDC(ServletRequest request) {
56  
57          MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request.getRemoteHost());
58  
59          if (request instanceof HttpServletRequest) {
60              HttpServletRequest httpServletRequest = (HttpServletRequest) request;
61              MDC.put(ClassicConstants.REQUEST_REQUEST_URI, httpServletRequest.getRequestURI());
62              StringBuffer requestURL = httpServletRequest.getRequestURL();
63              if (requestURL != null) {
64                  MDC.put(ClassicConstants.REQUEST_REQUEST_URL, requestURL.toString());
65              }
66              MDC.put(ClassicConstants.REQUEST_METHOD, httpServletRequest.getMethod());
67              MDC.put(ClassicConstants.REQUEST_QUERY_STRING, httpServletRequest.getQueryString());
68              MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest.getHeader("User-Agent"));
69              MDC.put(ClassicConstants.REQUEST_X_FORWARDED_FOR, httpServletRequest.getHeader("X-Forwarded-For"));
70          }
71  
72      }
73  
74      void clearMDC() {
75          MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
76          MDC.remove(ClassicConstants.REQUEST_REQUEST_URI);
77          MDC.remove(ClassicConstants.REQUEST_QUERY_STRING);
78          // removing possibly inexistent item is OK
79          MDC.remove(ClassicConstants.REQUEST_REQUEST_URL);
80          MDC.remove(ClassicConstants.REQUEST_METHOD);
81          MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
82          MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR);
83      }
84  
85      public void init(FilterConfig arg0) throws ServletException {
86          // do nothing
87      }
88  }