Class HttpHeaderNestedDiagnosticContextFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.web.filter.AbstractRequestLoggingFilter
de.bund.bva.isyfact.logging.http.HttpHeaderNestedDiagnosticContextFilter
All Implemented Interfaces:
javax.servlet.Filter, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.EnvironmentAware, org.springframework.core.env.EnvironmentCapable, org.springframework.web.context.ServletContextAware

public class HttpHeaderNestedDiagnosticContextFilter extends org.springframework.web.filter.AbstractRequestLoggingFilter
A servlet filter which reads the Correlation-ID from the http header correlationIdHttpHeaderName and makes it accessible to the logging framework. If the header is not set, a new Correlation-ID is generated.

Processing the Correlation-ID in a servlet filter ensures that it is set as early as possible within the request. Therefore all of spring's log messages contain the correct Correlation-ID.

The Configuration is done automatically by MdcFilterAutoConfiguration.

  • Field Summary

    Fields inherited from class org.springframework.web.filter.AbstractRequestLoggingFilter

    DEFAULT_AFTER_MESSAGE_PREFIX, DEFAULT_AFTER_MESSAGE_SUFFIX, DEFAULT_BEFORE_MESSAGE_PREFIX, DEFAULT_BEFORE_MESSAGE_SUFFIX

    Fields inherited from class org.springframework.web.filter.OncePerRequestFilter

    ALREADY_FILTERED_SUFFIX

    Fields inherited from class org.springframework.web.filter.GenericFilterBean

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    afterRequest(javax.servlet.http.HttpServletRequest request, String message)
    Removes the log message from the Slf4j MDC after the request is processed and logs the after-request message through SLF4J.
    protected void
    beforeRequest(javax.servlet.http.HttpServletRequest request, String message)
    Logs the before-request message through Slf4j and adds a message the Slf4j MDC before the request is processed.
    protected String
    getNestedDiagnosticContextMessage(javax.servlet.http.HttpServletRequest request)
    Determine the message to be pushed onto the SLF4J nested diagnostic context.
    void
    setCorrelationIdHttpHeaderName(String correlationIdHttpHeaderName)
    Sets the name of the http header with the Correlation-ID.

    Methods inherited from class org.springframework.web.filter.AbstractRequestLoggingFilter

    createMessage, doFilterInternal, getHeaderPredicate, getMaxPayloadLength, getMessagePayload, isIncludeClientInfo, isIncludeHeaders, isIncludePayload, isIncludeQueryString, setAfterMessagePrefix, setAfterMessageSuffix, setBeforeMessagePrefix, setBeforeMessageSuffix, setHeaderPredicate, setIncludeClientInfo, setIncludeHeaders, setIncludePayload, setIncludeQueryString, setMaxPayloadLength, shouldLog, shouldNotFilterAsyncDispatch

    Methods inherited from class org.springframework.web.filter.OncePerRequestFilter

    doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterErrorDispatch

    Methods inherited from class org.springframework.web.filter.GenericFilterBean

    addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HttpHeaderNestedDiagnosticContextFilter

      public HttpHeaderNestedDiagnosticContextFilter()
  • Method Details

    • setCorrelationIdHttpHeaderName

      public void setCorrelationIdHttpHeaderName(String correlationIdHttpHeaderName)
      Sets the name of the http header with the Correlation-ID.
      Parameters:
      correlationIdHttpHeaderName - the name of the http header with the Correlation-ID.
    • getNestedDiagnosticContextMessage

      protected String getNestedDiagnosticContextMessage(javax.servlet.http.HttpServletRequest request)
      Determine the message to be pushed onto the SLF4J nested diagnostic context.

      Default is a plain request log message without prefix or suffix.

      Parameters:
      request - current HTTP request
      Returns:
      the message to be pushed onto the Slf4j MDC
      See Also:
      • AbstractRequestLoggingFilter.createMessage(javax.servlet.http.HttpServletRequest, java.lang.String, java.lang.String)
    • beforeRequest

      protected void beforeRequest(javax.servlet.http.HttpServletRequest request, String message)
      Logs the before-request message through Slf4j and adds a message the Slf4j MDC before the request is processed.
      Specified by:
      beforeRequest in class org.springframework.web.filter.AbstractRequestLoggingFilter
    • afterRequest

      protected void afterRequest(javax.servlet.http.HttpServletRequest request, String message)
      Removes the log message from the Slf4j MDC after the request is processed and logs the after-request message through SLF4J.
      Specified by:
      afterRequest in class org.springframework.web.filter.AbstractRequestLoggingFilter