Class AbstractAccessLogValve
- java.lang.Object
-
- org.apache.catalina.util.LifecycleBase
-
- org.apache.catalina.util.LifecycleMBeanBase
-
- org.apache.catalina.valves.ValveBase
-
- org.apache.catalina.valves.AbstractAccessLogValve
-
- All Implemented Interfaces:
javax.management.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_configmodule.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 ifenableLookupsfor 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}ifor incoming headers%{xxx}ofor outgoing response headers%{xxx}cfor a specific cookie%{xxx}rxxx is an attribute in the ServletRequest%{xxx}sxxx is an attribute in the HttpSession%{xxx}txxx 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
conditionUnlessandconditionIfproperties. 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. Theconditionattribute is synonym forconditionUnlessand 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interfaceAbstractAccessLogValve.AccessLogElementAccessLogElement writes the partial message into the buffer.protected static classAbstractAccessLogValve.ByteSentElementwrite bytes sent, excluding HTTP headers - %b, %Bprotected static interfaceAbstractAccessLogValve.CachedElementMarks an AccessLogElement as needing to be have the value cached at the start of the request rather than just recorded at the end as the source data for the element may not be available at the end of the request.protected static classAbstractAccessLogValve.ConnectionStatusElementWrite connection status when response is completed - %Xprotected static classAbstractAccessLogValve.CookieElementwrite a specific cookie - %{xxx}cprotected classAbstractAccessLogValve.DateAndTimeElementwrite date and time, in configurable format (default CLF) - %t or %{format}tprotected static classAbstractAccessLogValve.DateFormatCacheCache structure for formatted timestamps based on seconds.protected static classAbstractAccessLogValve.ElapsedTimeElementwrite time taken to process the request - %D, %Tprotected static classAbstractAccessLogValve.FirstByteTimeElementwrite time until first byte is written (commit time) in millis - %Fprotected static classAbstractAccessLogValve.HeaderElementwrite incoming headers - %{xxx}iprotected classAbstractAccessLogValve.HostElementwrite remote host name - %hprotected static classAbstractAccessLogValve.HttpStatusCodeElementwrite HTTP status code of the response - %sprotected static classAbstractAccessLogValve.LocalAddrElementwrite local IP address - %Aprotected classAbstractAccessLogValve.LocalServerNameElementwrite local server name - %vprotected static classAbstractAccessLogValve.LogicalUserNameElementwrite remote logical username from identd (always returns '-') - %lprotected static classAbstractAccessLogValve.MethodElementwrite request method (GET, POST, etc.) - %mprotected classAbstractAccessLogValve.PortElementwrite local or remote port for request connection - %p and %{xxx}pprotected classAbstractAccessLogValve.ProtocolElementwrite request protocol - %Hprotected static classAbstractAccessLogValve.QueryElementwrite Query string (prepended with a '?'protected classAbstractAccessLogValve.RemoteAddrElementwrite remote IP address - %aprotected static classAbstractAccessLogValve.RequestAttributeElementwrite an attribute in the ServletRequest - %{xxx}rprotected static classAbstractAccessLogValve.RequestElementwrite first line of the request (method and request URI) - %rprotected static classAbstractAccessLogValve.RequestURIElementwrite requested URL path - %Uprotected static classAbstractAccessLogValve.ResponseHeaderElementwrite a specific response header - %{xxx}oprotected static classAbstractAccessLogValve.SessionAttributeElementwrite an attribute in the HttpSession - %{xxx}sprotected static classAbstractAccessLogValve.SessionIdElementwrite user session ID - %Sprotected static classAbstractAccessLogValve.StringElementwrite any stringprotected static classAbstractAccessLogValve.ThreadNameElementwrite thread name - %Iprotected static classAbstractAccessLogValve.UserElementwrite remote user that was authenticated (if any), else '-' - %u-
Nested classes/interfaces inherited from interface org.apache.catalina.Lifecycle
Lifecycle.SingleUse
-
-
Field Summary
Fields Modifier and Type Field Description protected AbstractAccessLogValve.CachedElement[]cachedElementsArray of elements where the value needs to be cached at the start of the request.protected java.lang.StringconditionAre we doing conditional logging. default null.protected java.lang.StringconditionIfAre we doing conditional logging. default null.protected booleanenabledenabled this componentprotected java.util.LocalelocaleLocale used to format timestamps in log entries and in log file name suffix.protected java.lang.StringlocaleNameName of locale used to format timestamps in log entries and in log file name suffix.protected AbstractAccessLogValve.AccessLogElement[]logElementsArray of AccessLogElement, they will be used to make log message.protected java.lang.StringpatternThe pattern used to format our access log lines.protected booleanrequestAttributesEnabledShould this valve use request attributes for IP address, hostname, protocol and port used for the request.-
Fields inherited from class org.apache.catalina.valves.ValveBase
asyncSupported, container, containerLog, next, sm
-
Fields inherited from class org.apache.catalina.util.LifecycleMBeanBase
mserver
-
Fields inherited from interface org.apache.catalina.AccessLog
PROTOCOL_ATTRIBUTE, REMOTE_ADDR_ATTRIBUTE, REMOTE_HOST_ATTRIBUTE, SERVER_NAME_ATTRIBUTE, SERVER_PORT_ATTRIBUTE
-
Fields inherited from interface org.apache.catalina.Lifecycle
AFTER_DESTROY_EVENT, AFTER_INIT_EVENT, AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_DESTROY_EVENT, BEFORE_INIT_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, CONFIGURE_START_EVENT, CONFIGURE_STOP_EVENT, PERIODIC_EVENT, START_EVENT, STOP_EVENT
-
-
Constructor Summary
Constructors Constructor Description AbstractAccessLogValve()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected AbstractAccessLogValve.AccessLogElementcreateAccessLogElement(char pattern)Create an AccessLogElement implementation.protected AbstractAccessLogValve.AccessLogElementcreateAccessLogElement(java.lang.String name, char pattern)Create an AccessLogElement implementation which needs an element name.protected AbstractAccessLogValve.AccessLogElement[]createLogElements()Parse pattern string and create the array of AccessLogElement.protected static voidescapeAndAppend(java.lang.String input, java.io.CharArrayWriter dest)protected static java.util.LocalefindLocale(java.lang.String name, java.util.Locale fallback)Find a locale by name.java.lang.StringgetCondition()Return whether the attribute name to look for when performing conditional logging.java.lang.StringgetConditionIf()Return whether the attribute name to look for when performing conditional logging.java.lang.StringgetConditionUnless()Return whether the attribute name to look for when performing conditional logging.booleangetEnabled()booleangetIpv6Canonical()java.lang.StringgetLocale()Return the locale used to format timestamps in log entries and in log file name suffix.intgetMaxLogMessageBufferSize()java.lang.StringgetPattern()booleangetRequestAttributesEnabled()voidinvoke(Request request, Response response)Log a message summarizing the specified request and response, according to the format specified by thepatternproperty.protected abstract voidlog(java.io.CharArrayWriter message)Log the specified message.voidlog(Request request, Response response, long time)Add the request/response to the access log using the specified processing time.voidsetCondition(java.lang.String condition)Set the ServletRequest.attribute to look for to perform conditional logging.voidsetConditionIf(java.lang.String condition)Set the ServletRequest.attribute to look for to perform conditional logging.voidsetConditionUnless(java.lang.String condition)Set the ServletRequest.attribute to look for to perform conditional logging.voidsetEnabled(boolean enabled)voidsetIpv6Canonical(boolean ipv6Canonical)voidsetLocale(java.lang.String localeName)Set the locale used to format timestamps in log entries and in log file name suffix.voidsetMaxLogMessageBufferSize(int maxLogMessageBufferSize)voidsetPattern(java.lang.String pattern)Set the format pattern, first translating any recognized alias.voidsetRequestAttributesEnabled(boolean requestAttributesEnabled)Should this valve use request attributes for IP address, hostname, protocol and port used for the request?-
Methods inherited from class org.apache.catalina.valves.ValveBase
backgroundProcess, getContainer, getDomainInternal, getNext, getObjectNameKeyProperties, initInternal, isAsyncSupported, setAsyncSupported, setContainer, setNext, startInternal, stopInternal, toString
-
Methods inherited from class org.apache.catalina.util.LifecycleMBeanBase
destroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister, unregister
-
Methods inherited from class org.apache.catalina.util.LifecycleBase
addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stop
-
-
-
-
Field Detail
-
enabled
protected boolean enabled
enabled this component
-
pattern
protected java.lang.String pattern
The pattern used to format our access log lines.
-
condition
protected java.lang.String condition
Are we doing conditional logging. default null. It is the value ofconditionUnlessproperty.
-
conditionIf
protected java.lang.String conditionIf
Are we doing conditional logging. default null. It is the value ofconditionIfproperty.
-
localeName
protected java.lang.String localeName
Name of locale used to format timestamps in log entries and in log file name suffix.
-
locale
protected java.util.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 isfalse.- See Also:
setRequestAttributesEnabled(boolean)
-
-
Method Detail
-
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
false.- Specified by:
setRequestAttributesEnabledin interfaceAccessLog- Parameters:
requestAttributesEnabled-truecauses the attributes to be used,falsecauses the original values to be used.
-
getRequestAttributesEnabled
public boolean getRequestAttributesEnabled()
- Specified by:
getRequestAttributesEnabledin interfaceAccessLog- Returns:
trueif the attributes will be logged, otherwisefalse- See Also:
AccessLog.setRequestAttributesEnabled(boolean)
-
getEnabled
public boolean getEnabled()
- Returns:
- the enabled flag.
-
setEnabled
public void setEnabled(boolean enabled)
- Parameters:
enabled- The enabled to set.
-
getPattern
public java.lang.String getPattern()
- Returns:
- the format pattern.
-
setPattern
public void setPattern(java.lang.String pattern)
Set the format pattern, first translating any recognized alias.- Parameters:
pattern- The new pattern
-
getCondition
public java.lang.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(java.lang.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 java.lang.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(java.lang.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 java.lang.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(java.lang.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 java.lang.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(java.lang.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 java.io.IOException, ServletException
Log a message summarizing the specified request and response, according to the format specified by thepatternproperty.- Specified by:
invokein interfaceValve- Parameters:
request- Request being processedresponse- Response being processed- Throws:
java.io.IOException- if an input/output error has occurredServletException- if a servlet error has occurred
-
log
public void log(Request request, Response response, long time)
Description copied from interface:AccessLogAdd the request/response to the access log using the specified processing time.
-
log
protected abstract void log(java.io.CharArrayWriter message)
Log the specified message.- Parameters:
message- Message to be logged. This object will be recycled by the calling method.
-
findLocale
protected static java.util.Locale findLocale(java.lang.String name, java.util.Locale fallback)Find a locale by name.- Parameters:
name- The locale namefallback- 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(java.lang.String name, char pattern)
Create an AccessLogElement implementation which needs an element name.- Parameters:
name- Header namepattern- 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(java.lang.String input, java.io.CharArrayWriter dest)
-
-