Class AbstractRequestLoggingFilter

All Implemented Interfaces:
Filter, Aware, BeanNameAware, DisposableBean, InitializingBean, EnvironmentAware, EnvironmentCapable, ServletContextAware
Direct Known Subclasses:
CommonsRequestLoggingFilter, ServletContextRequestLoggingFilter

public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
Base class for Filters that perform logging operations before and after a request is processed.

Subclasses should override the beforeRequest(HttpServletRequest, String) and afterRequest(HttpServletRequest, String) methods to perform the actual logging around the request.

Subclasses are passed the message to write to the log in the beforeRequest and afterRequest methods. By default, only the URI of the request is logged. However, setting the includeQueryString property to true will cause the query string of the request to be included also; this can be further extended through includeClientInfo and includeHeaders. The payload (body content) of the request can be logged via the includePayload flag: Note that this will only log the part of the payload which has actually been read, not necessarily the entire body of the request.

Prefixes and suffixes for the before and after messages can be configured using the beforeMessagePrefix, afterMessagePrefix, beforeMessageSuffix and afterMessageSuffix properties.

Since:
1.2.5
Author:
Rob Harrop, Juergen Hoeller, Rossen Stoyanchev
See Also:
  • Field Details

    • DEFAULT_BEFORE_MESSAGE_PREFIX

      public static final String DEFAULT_BEFORE_MESSAGE_PREFIX
      The default value prepended to the log message written before a request is processed.
      See Also:
    • DEFAULT_BEFORE_MESSAGE_SUFFIX

      public static final String DEFAULT_BEFORE_MESSAGE_SUFFIX
      The default value appended to the log message written before a request is processed.
      See Also:
    • DEFAULT_AFTER_MESSAGE_PREFIX

      public static final String DEFAULT_AFTER_MESSAGE_PREFIX
      The default value prepended to the log message written after a request is processed.
      See Also:
    • DEFAULT_AFTER_MESSAGE_SUFFIX

      public static final String DEFAULT_AFTER_MESSAGE_SUFFIX
      The default value appended to the log message written after a request is processed.
      See Also:
  • Constructor Details

    • AbstractRequestLoggingFilter

      public AbstractRequestLoggingFilter()
  • Method Details

    • setIncludeQueryString

      public void setIncludeQueryString(boolean includeQueryString)
      Set whether the query string should be included in the log message.

      Should be configured using an <init-param> for parameter name "includeQueryString" in the filter definition in web.xml.

    • isIncludeQueryString

      protected boolean isIncludeQueryString()
      Return whether the query string should be included in the log message.
    • setIncludeClientInfo

      public void setIncludeClientInfo(boolean includeClientInfo)
      Set whether the client address and session id should be included in the log message.

      Should be configured using an <init-param> for parameter name "includeClientInfo" in the filter definition in web.xml.

    • isIncludeClientInfo

      protected boolean isIncludeClientInfo()
      Return whether the client address and session id should be included in the log message.
    • setIncludeHeaders

      public void setIncludeHeaders(boolean includeHeaders)
      Set whether the request headers should be included in the log message.

      Should be configured using an <init-param> for parameter name "includeHeaders" in the filter definition in web.xml.

      Since:
      4.3
    • isIncludeHeaders

      protected boolean isIncludeHeaders()
      Return whether the request headers should be included in the log message.
      Since:
      4.3
    • setIncludePayload

      public void setIncludePayload(boolean includePayload)
      Set whether the request payload (body) should be included in the log message.

      Should be configured using an <init-param> for parameter name "includePayload" in the filter definition in web.xml.

      Since:
      3.0
    • isIncludePayload

      protected boolean isIncludePayload()
      Return whether the request payload (body) should be included in the log message.
      Since:
      3.0
    • setHeaderPredicate

      public void setHeaderPredicate(@Nullable Predicate<String> headerPredicate)
      Configure a predicate for selecting which headers should be logged if setIncludeHeaders(boolean) is set to true.

      By default this is not set in which case all headers are logged.

      Parameters:
      headerPredicate - the predicate to use
      Since:
      5.2
    • getHeaderPredicate

      @Nullable protected Predicate<String> getHeaderPredicate()
      The configured headerPredicate.
      Since:
      5.2
    • setMaxPayloadLength

      public void setMaxPayloadLength(int maxPayloadLength)
      Set the maximum length of the payload body to be included in the log message. Default is 50 characters.
      Since:
      3.0
    • getMaxPayloadLength

      protected int getMaxPayloadLength()
      Return the maximum length of the payload body to be included in the log message.
      Since:
      3.0
    • setBeforeMessagePrefix

      public void setBeforeMessagePrefix(String beforeMessagePrefix)
      Set the value that should be prepended to the log message written before a request is processed.
    • setBeforeMessageSuffix

      public void setBeforeMessageSuffix(String beforeMessageSuffix)
      Set the value that should be appended to the log message written before a request is processed.
    • setAfterMessagePrefix

      public void setAfterMessagePrefix(String afterMessagePrefix)
      Set the value that should be prepended to the log message written after a request is processed.
    • setAfterMessageSuffix

      public void setAfterMessageSuffix(String afterMessageSuffix)
      Set the value that should be appended to the log message written after a request is processed.
    • shouldNotFilterAsyncDispatch

      protected boolean shouldNotFilterAsyncDispatch()
      The default value is "false" so that the filter may log a "before" message at the start of request processing and an "after" message at the end from when the last asynchronously dispatched thread is exiting.
      Overrides:
      shouldNotFilterAsyncDispatch in class OncePerRequestFilter
    • doFilterInternal

      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
      Forwards the request to the next filter in the chain and delegates down to the subclasses to perform the actual request logging both before and after the request is processed.
      Specified by:
      doFilterInternal in class OncePerRequestFilter
      Throws:
      ServletException
      IOException
      See Also:
    • createMessage

      protected String createMessage(HttpServletRequest request, String prefix, String suffix)
      Create a log message for the given request, prefix and suffix.

      If includeQueryString is true, then the inner part of the log message will take the form request_uri?query_string; otherwise the message will simply be of the form request_uri.

      The final message is composed of the inner part as described and the supplied prefix and suffix.

    • getMessagePayload

      @Nullable protected String getMessagePayload(HttpServletRequest request)
      Extracts the message payload portion of the message created by createMessage(HttpServletRequest, String, String) when isIncludePayload() returns true.
      Since:
      5.0.3
    • shouldLog

      protected boolean shouldLog(HttpServletRequest request)
      Determine whether to call the beforeRequest(jakarta.servlet.http.HttpServletRequest, java.lang.String)/afterRequest(jakarta.servlet.http.HttpServletRequest, java.lang.String) methods for the current request, i.e. whether logging is currently active (and the log message is worth building).

      The default implementation always returns true. Subclasses may override this with a log level check.

      Parameters:
      request - current HTTP request
      Returns:
      true if the before/after method should get called; false otherwise
      Since:
      4.1.5
    • beforeRequest

      protected abstract void beforeRequest(HttpServletRequest request, String message)
      Concrete subclasses should implement this method to write a log message before the request is processed.
      Parameters:
      request - current HTTP request
      message - the message to log
    • afterRequest

      protected abstract void afterRequest(HttpServletRequest request, String message)
      Concrete subclasses should implement this method to write a log message after the request is processed.
      Parameters:
      request - current HTTP request
      message - the message to log