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 jakarta.servlet.Filter;
19  import jakarta.servlet.FilterChain;
20  import jakarta.servlet.FilterConfig;
21  import jakarta.servlet.ServletException;
22  import jakarta.servlet.ServletRequest;
23  import jakarta.servlet.ServletResponse;
24  import jakarta.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)
46              throws IOException, ServletException {
47  
48          insertIntoMDC(request);
49          try {
50              chain.doFilter(request, response);
51          } finally {
52              clearMDC();
53          }
54      }
55  
56      void insertIntoMDC(ServletRequest request) {
57  
58          MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request.getRemoteHost());
59  
60          if (request instanceof HttpServletRequest) {
61              HttpServletRequest httpServletRequest = (HttpServletRequest) request;
62              MDC.put(ClassicConstants.REQUEST_REQUEST_URI, httpServletRequest.getRequestURI());
63              StringBuffer requestURL = httpServletRequest.getRequestURL();
64              if (requestURL != null) {
65                  MDC.put(ClassicConstants.REQUEST_REQUEST_URL, requestURL.toString());
66              }
67              MDC.put(ClassicConstants.REQUEST_METHOD, httpServletRequest.getMethod());
68              MDC.put(ClassicConstants.REQUEST_QUERY_STRING, httpServletRequest.getQueryString());
69              MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest.getHeader("User-Agent"));
70              MDC.put(ClassicConstants.REQUEST_X_FORWARDED_FOR, httpServletRequest.getHeader("X-Forwarded-For"));
71          }
72  
73      }
74  
75      void clearMDC() {
76          MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
77          MDC.remove(ClassicConstants.REQUEST_REQUEST_URI);
78          MDC.remove(ClassicConstants.REQUEST_QUERY_STRING);
79          // removing possibly nonexistent item is OK
80          MDC.remove(ClassicConstants.REQUEST_REQUEST_URL);
81          MDC.remove(ClassicConstants.REQUEST_METHOD);
82          MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
83          MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR);
84      }
85  
86      public void init(FilterConfig arg0) throws ServletException {
87          // do nothing
88      }
89  }