Class AbstractAccessLogValve

All Implemented Interfaces:
MBeanRegistration, AccessLog, Contained, JmxEnabled, Lifecycle, Valve
Direct Known Subclasses:
AccessLogValve

public abstract class AbstractAccessLogValve extends ValveBase implements AccessLog

Abstract implementation of the Valve interface that generates a web server access log with the detailed line contents matching a configurable pattern. The syntax of the available patterns is similar to that supported by the Apache HTTP Server mod_log_config module.

Patterns for the logged message may include constant text or any of the following replacement strings, for which the corresponding information from the specified Response is substituted:

  • %a - Remote IP address
  • %A - Local IP address
  • %b - Bytes sent, excluding HTTP headers, or '-' if no bytes were sent
  • %B - Bytes sent, excluding HTTP headers
  • %h - Remote host name (or IP address if enableLookups for the connector is false)
  • %H - Request protocol
  • %l - Remote logical username from identd (always returns '-')
  • %m - Request method
  • %p - Local port
  • %q - Query string (prepended with a '?' if it exists, otherwise an empty string
  • %r - First line of the request
  • %s - HTTP status code of the response
  • %S - User session ID
  • %t - Date and time, in Common Log Format format
  • %u - Remote user that was authenticated
  • %U - Requested URL path
  • %v - Local server name
  • %D - Time taken to process the request, in millis
  • %T - Time taken to process the request, in seconds
  • %F - Time taken to commit the response, in millis
  • %I - current Request thread name (can compare later with stacktraces)
  • %X - Connection status when response is completed:
    • X = Connection aborted before the response completed.
    • + = Connection may be kept alive after the response is sent.
    • - = Connection will be closed after the response is sent.

In addition, the caller can specify one of the following aliases for commonly utilized patterns:

  • common - %h %l %u %t "%r" %s %b
  • combined - %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"

There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest.
It is modeled after the Apache HTTP Server log configuration syntax:

  • %{xxx}i for incoming headers
  • %{xxx}o for outgoing response headers
  • %{xxx}c for a specific cookie
  • %{xxx}r xxx is an attribute in the ServletRequest
  • %{xxx}s xxx is an attribute in the HttpSession
  • %{xxx}t xxx is an enhanced SimpleDateFormat pattern (see Configuration Reference document for details on supported time patterns)

Conditional logging is also supported. This can be done with the conditionUnless and conditionIf properties. If the value returned from ServletRequest.getAttribute(conditionUnless) yields a non-null value, the logging will be skipped. If the value returned from ServletRequest.getAttribute(conditionIf) yields the null value, the logging will be skipped. The condition attribute is synonym for conditionUnless and is provided for backwards compatibility.

For extended attributes coming from a getAttribute() call, it is you responsibility to ensure there are no newline or control characters.

Author:
Craig R. McClanahan, Jason Brittain, Remy Maucherat, Takayuki Kaneko, Peter Rossbach
  • Field Details

    • enabled

      protected boolean enabled
      enabled this component
    • pattern

      protected String pattern
      The pattern used to format our access log lines.
    • condition

      protected String condition
      Are we doing conditional logging. default null. It is the value of conditionUnless property.
    • conditionIf

      protected String conditionIf
      Are we doing conditional logging. default null. It is the value of conditionIf property.
    • localeName

      protected String localeName
      Name of locale used to format timestamps in log entries and in log file name suffix.
    • locale

      protected Locale locale
      Locale used to format timestamps in log entries and in log file name suffix.
    • logElements

      protected AbstractAccessLogValve.AccessLogElement[] logElements
      Array of AccessLogElement, they will be used to make log message.
    • cachedElements

      protected AbstractAccessLogValve.CachedElement[] cachedElements
      Array of elements where the value needs to be cached at the start of the request.
    • requestAttributesEnabled

      protected boolean requestAttributesEnabled
      Should this valve use request attributes for IP address, hostname, protocol and port used for the request. Default is false.
      See Also:
  • Constructor Details

    • AbstractAccessLogValve

      public AbstractAccessLogValve()
  • Method Details

    • getMaxLogMessageBufferSize

      public int getMaxLogMessageBufferSize()
    • setMaxLogMessageBufferSize

      public void setMaxLogMessageBufferSize(int maxLogMessageBufferSize)
    • getIpv6Canonical

      public boolean getIpv6Canonical()
    • setIpv6Canonical

      public void setIpv6Canonical(boolean ipv6Canonical)
    • setRequestAttributesEnabled

      public void setRequestAttributesEnabled(boolean requestAttributesEnabled)
      Should this valve use request attributes for IP address, hostname, protocol and port used for the request? The attributes used are:
      • org.apache.catalina.RemoteAddr
      • org.apache.catalina.RemoteHost
      • org.apache.catalina.Protocol
      • org.apache.catalina.ServerName
      • org.apache.catalina.ServerPost
      Default is false.
      Specified by:
      setRequestAttributesEnabled in interface AccessLog
      Parameters:
      requestAttributesEnabled - true causes the attributes to be used, false causes the original values to be used.
    • getRequestAttributesEnabled

      public boolean getRequestAttributesEnabled()
      Specified by:
      getRequestAttributesEnabled in interface AccessLog
      Returns:
      true if the attributes will be logged, otherwise false
      See Also:
    • getEnabled

      public boolean getEnabled()
      Returns:
      the enabled flag.
    • setEnabled

      public void setEnabled(boolean enabled)
      Parameters:
      enabled - The enabled to set.
    • getPattern

      public String getPattern()
      Returns:
      the format pattern.
    • setPattern

      public void setPattern(String pattern)
      Set the format pattern, first translating any recognized alias.
      Parameters:
      pattern - The new pattern
    • getCondition

      public String getCondition()
      Return whether the attribute name to look for when performing conditional logging. If null, every request is logged.
      Returns:
      the attribute name
    • setCondition

      public void setCondition(String condition)
      Set the ServletRequest.attribute to look for to perform conditional logging. Set to null to log everything.
      Parameters:
      condition - Set to null to log everything
    • getConditionUnless

      public String getConditionUnless()
      Return whether the attribute name to look for when performing conditional logging. If null, every request is logged.
      Returns:
      the attribute name
    • setConditionUnless

      public void setConditionUnless(String condition)
      Set the ServletRequest.attribute to look for to perform conditional logging. Set to null to log everything.
      Parameters:
      condition - Set to null to log everything
    • getConditionIf

      public String getConditionIf()
      Return whether the attribute name to look for when performing conditional logging. If null, every request is logged.
      Returns:
      the attribute name
    • setConditionIf

      public void setConditionIf(String condition)
      Set the ServletRequest.attribute to look for to perform conditional logging. Set to null to log everything.
      Parameters:
      condition - Set to null to log everything
    • getLocale

      public String getLocale()
      Return the locale used to format timestamps in log entries and in log file name suffix.
      Returns:
      the locale
    • setLocale

      public void setLocale(String localeName)
      Set the locale used to format timestamps in log entries and in log file name suffix. Changing the locale is only supported as long as the AccessLogValve has not logged anything. Changing the locale later can lead to inconsistent formatting.
      Parameters:
      localeName - The locale to use.
    • invoke

      public void invoke(Request request, Response response) throws IOException, ServletException
      Description copied from interface: Valve

      Perform request processing as required by this Valve.

      An individual Valve MAY perform the following actions, in the specified order:

      • Examine and/or modify the properties of the specified Request and Response.
      • Examine the properties of the specified Request, completely generate the corresponding Response, and return control to the caller.
      • Examine the properties of the specified Request and Response, wrap either or both of these objects to supplement their functionality, and pass them on.
      • If the corresponding Response was not generated (and control was not returned, call the next Valve in the pipeline (if there is one) by executing getNext().invoke().
      • Examine, but not modify, the properties of the resulting Response (which was created by a subsequently invoked Valve or Container).

      A Valve MUST NOT do any of the following things:

      • Change request properties that have already been used to direct the flow of processing control for this request (for instance, trying to change the virtual host to which a Request should be sent from a pipeline attached to a Host or Context in the standard implementation).
      • Create a completed Response AND pass this Request and Response on to the next Valve in the pipeline.
      • Consume bytes from the input stream associated with the Request, unless it is completely generating the response, or wrapping the request before passing it on.
      • Modify the HTTP headers included with the Response after the getNext().invoke() method has returned.
      • Perform any actions on the output stream associated with the specified Response after the getNext().invoke() method has returned.
      Specified by:
      invoke in interface Valve
      Parameters:
      request - The servlet request to be processed
      response - The servlet response to be created
      Throws:
      IOException - if an input/output error occurs, or is thrown by a subsequently invoked Valve, Filter, or Servlet
      ServletException - if a servlet error occurs, or is thrown by a subsequently invoked Valve, Filter, or Servlet
    • log

      public void log(Request request, Response response, long time)
      Description copied from interface: AccessLog
      Add the request/response to the access log using the specified processing time.
      Specified by:
      log in interface AccessLog
      Parameters:
      request - Request (associated with the response) to log
      response - Response (associated with the request) to log
      time - Time taken to process the request/response in milliseconds (use 0 if not known)
    • log

      protected abstract void log(CharArrayWriter message)
      Log the specified message.
      Parameters:
      message - Message to be logged. This object will be recycled by the calling method.
    • findLocale

      protected static Locale findLocale(String name, Locale fallback)
      Find a locale by name.
      Parameters:
      name - The locale name
      fallback - Fallback locale if the name is not found
      Returns:
      the locale object
    • createLogElements

      protected AbstractAccessLogValve.AccessLogElement[] createLogElements()
      Parse pattern string and create the array of AccessLogElement.
      Returns:
      the log elements array
    • createAccessLogElement

      protected AbstractAccessLogValve.AccessLogElement createAccessLogElement(String name, char pattern)
      Create an AccessLogElement implementation which needs an element name.
      Parameters:
      name - Header name
      pattern - char in the log pattern
      Returns:
      the log element
    • createAccessLogElement

      protected AbstractAccessLogValve.AccessLogElement createAccessLogElement(char pattern)
      Create an AccessLogElement implementation.
      Parameters:
      pattern - char in the log pattern
      Returns:
      the log element
    • escapeAndAppend

      protected static void escapeAndAppend(String input, CharArrayWriter dest)