Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
package ch.qos.logback.classic.helpers;
A servlet filter that inserts various values retrieved from the incoming http request into the MDC.

The values are removed after the request is processed.

Author(s):
Ceki Gülcü
public class MDCInsertingServletFilter implements Filter {
  public void destroy() {
    // do nothing
  }
  public void doFilter(ServletRequest requestServletResponse response,
                       FilterChain chainthrows IOExceptionServletException {
    insertIntoMDC(request);
    try {
      chain.doFilter(requestresponse);
    } finally {
      clearMDC();
    }
  }
  void insertIntoMDC(ServletRequest request) {
            .getRemoteHost());
    if (request instanceof HttpServletRequest) {
      HttpServletRequest httpServletRequest = (HttpServletRequestrequest;
      MDC.put(.httpServletRequest
              .getRequestURI());
      StringBuffer requestURL = httpServletRequest.getRequestURL();
      if (requestURL != null) {
        MDC.put(.requestURL.toString());
      }
      MDC.put(.httpServletRequest.getQueryString());
      MDC.put(.httpServletRequest
              .getHeader("User-Agent"));
      MDC.put(.httpServletRequest
              .getHeader("X-Forwarded-For"));
    }
  }
  void clearMDC() {
    // removing possibly inexistent item is OK
  }
  public void init(FilterConfig arg0throws ServletException {
    // do nothing
  }
New to GrepCode? Check out our FAQ X