View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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,
46                         FilterChain chain) throws IOException, ServletException {
47  
48  
49      insertIntoMDC(request);
50      try {
51        chain.doFilter(request, response);
52      } finally {
53        clearMDC();
54      }
55    }
56  
57    void insertIntoMDC(ServletRequest request) {
58  
59      MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request
60              .getRemoteHost());
61  
62      if (request instanceof HttpServletRequest) {
63        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
64        MDC.put(ClassicConstants.REQUEST_REQUEST_URI, httpServletRequest
65                .getRequestURI());
66        StringBuffer requestURL = httpServletRequest.getRequestURL();
67        if (requestURL != null) {
68          MDC.put(ClassicConstants.REQUEST_REQUEST_URL, requestURL.toString());
69        }
70        MDC.put(ClassicConstants.REQUEST_QUERY_STRING, httpServletRequest.getQueryString());
71        MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest
72                .getHeader("User-Agent"));
73        MDC.put(ClassicConstants.REQUEST_X_FORWARDED_FOR, httpServletRequest
74                .getHeader("X-Forwarded-For"));
75      }
76  
77    }
78  
79    void clearMDC() {
80      MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
81      MDC.remove(ClassicConstants.REQUEST_REQUEST_URI);
82      MDC.remove(ClassicConstants.REQUEST_QUERY_STRING);
83      // removing possibly inexistent item is OK
84      MDC.remove(ClassicConstants.REQUEST_REQUEST_URL);
85      MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
86      MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR);
87    }
88  
89    public void init(FilterConfig arg0) throws ServletException {
90      // do nothing
91    }
92  }