org.apache.catalina.valves

Class AccessLogValve

public class AccessLogValve extends ValveBase implements Lifecycle

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 mod_log_config module. As an additional feature, automatic rollover of log files when the date changes is also supported.

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:

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

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 syntax:

Conditional logging is also supported. This can be done with the condition property. If the value returned from ServletRequest.getAttribute(condition) yields a non-null value. The logging will be skipped.

Version: $Revision: 832123 $ $Date: 2009-11-02 22:57:21 +0100 (Mon, 02 Nov 2009) $

Author: Craig R. McClanahan Jason Brittain Remy Maucherat Takayuki Kaneko Peter Rossbach

Nested Class Summary
protected interfaceAccessLogValve.AccessLogElement
AccessLogElement writes the partial message into the buffer.
protected classAccessLogValve.ByteSentElement
write bytes sent, excluding HTTP headers - %b, %B
protected classAccessLogValve.CookieElement
write a specific cookie - %{xxx}c
protected classAccessLogValve.DateAndTimeElement
write date and time, in Common Log Format - %t
protected classAccessLogValve.ElapsedTimeElement
write time taken to process the request - %D, %T
protected classAccessLogValve.HeaderElement
write incoming headers - %{xxx}i
protected classAccessLogValve.HostElement
write remote host name - %h
protected classAccessLogValve.HttpStatusCodeElement
write HTTP status code of the response - %s
protected static classAccessLogValve.LocalAddrElement
write local IP address - %A
protected classAccessLogValve.LocalPortElement
write local port on which this request was received - %p
protected classAccessLogValve.LocalServerNameElement
write local server name - %v
protected classAccessLogValve.LogicalUserNameElement
write remote logical username from identd (always returns '-') - %l
protected classAccessLogValve.MethodElement
write request method (GET, POST, etc.)
protected classAccessLogValve.ProtocolElement
write request protocol - %H
protected classAccessLogValve.QueryElement
write Query string (prepended with a '?
protected classAccessLogValve.RemoteAddrElement
write remote IP address - %a
protected classAccessLogValve.RequestAttributeElement
write an attribute in the ServletRequest - %{xxx}r
protected classAccessLogValve.RequestElement
write first line of the request (method and request URI) - %r
protected classAccessLogValve.RequestURIElement
write requested URL path - %U
protected classAccessLogValve.ResponseHeaderElement
write a specific response header - %{xxx}o
protected classAccessLogValve.SessionAttributeElement
write an attribute in the HttpSession - %{xxx}s
protected classAccessLogValve.SessionIdElement
write user session ID - %S
protected classAccessLogValve.StringElement
write any string
protected classAccessLogValve.ThreadNameElement
write thread name - %I
protected classAccessLogValve.UserElement
write remote user that was authenticated (if any), else '-' - %u
Field Summary
protected Stringcondition
Are we doing conditional logging. default false.
protected FilecurrentLogFile
The current log file we are writing to.
protected booleanenabled
enabled this component
protected StringfileDateFormat
Date format to place in log file name.
protected SimpleDateFormatfileDateFormatter
A date formatter to format a Date into a date in the format "yyyy-MM-dd".
protected static Stringinfo
The descriptive information about this implementation.
protected LifecycleSupportlifecycle
The lifecycle event support for this component.
protected AccessLogValve.AccessLogElement[]logElements
Array of AccessLogElement, they will be used to make log message.
protected static String[]months
The set of month abbreviations for log messages.
protected Stringpattern
The pattern used to format our access log lines.
protected Stringprefix
The prefix that is added to log file filenames.
protected booleanrotatable
Should we rotate our log file?
protected StringManagersm
The string manager for this package.
protected booleanstarted
Has this component been started yet?
protected Stringsuffix
The suffix that is added to log file filenames.
protected PrintWriterwriter
The PrintWriter to which we are currently logging, if any.
Method Summary
voidaddLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.
voidbackgroundProcess()
Execute a periodic task, such as reloading, etc.
protected AccessLogValve.AccessLogElement[]createLogElements()
parse pattern string and create the array of AccessLogElement
LifecycleListener[]findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle.
StringgetCondition()
Return whether the attribute name to look for when performing conditional loggging.
StringgetDirectory()
Return the directory in which we create log files.
booleangetEnabled()
StringgetFileDateFormat()
Return the date format date based log rotation.
StringgetInfo()
Return descriptive information about this implementation.
StringgetPattern()
Return the format pattern.
StringgetPrefix()
Return the log file prefix.
StringgetSuffix()
Return the log file suffix.
voidinvoke(Request request, Response response)
Log a message summarizing the specified request and response, according to the format specified by the pattern property.
booleanisBuffered()
Is the logging buffered
booleanisCheckExists()
Check for file existence before logging.
booleanisResolveHosts()
Get the value of the resolve hosts flag.
booleanisRotatable()
Should we rotate the logs
voidlog(String message)
Log the specified message to the log file, switching files if the date has changed since the previous log call.
protected voidopen()
Open the new log file for the date specified by dateStamp.
voidremoveLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.
booleanrotate(String newFileName)
Rename the existing log file to something else.
voidsetBuffered(boolean buffered)
Set the value if the logging should be buffered
voidsetCheckExists(boolean checkExists)
Set whether to check for log file existence before logging.
voidsetCondition(String condition)
Set the ServletRequest.attribute to look for to perform conditional logging.
voidsetDirectory(String directory)
Set the directory in which we create log files.
voidsetEnabled(boolean enabled)
voidsetFileDateFormat(String fileDateFormat)
Set the date format date based log rotation.
voidsetPattern(String pattern)
Set the format pattern, first translating any recognized alias.
voidsetPrefix(String prefix)
Set the log file prefix.
voidsetResolveHosts(boolean resolveHosts)
Set the resolve hosts flag.
voidsetRotatable(boolean rotatable)
Set the value is we should we rotate the logs
voidsetSuffix(String suffix)
Set the log file suffix.
voidstart()
Prepare for the beginning of active use of the public methods of this component.
voidstop()
Gracefully terminate the active use of the public methods of this component.

Field Detail

condition

protected String condition
Are we doing conditional logging. default false.

currentLogFile

protected File currentLogFile
The current log file we are writing to. Helpful when checkExists is true.

enabled

protected boolean enabled
enabled this component

fileDateFormat

protected String fileDateFormat
Date format to place in log file name. Use at your own risk!

fileDateFormatter

protected SimpleDateFormat fileDateFormatter
A date formatter to format a Date into a date in the format "yyyy-MM-dd".

info

protected static final String info
The descriptive information about this implementation.

lifecycle

protected LifecycleSupport lifecycle
The lifecycle event support for this component.

logElements

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

months

protected static final String[] months
The set of month abbreviations for log messages.

pattern

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

prefix

protected String prefix
The prefix that is added to log file filenames.

rotatable

protected boolean rotatable
Should we rotate our log file? Default is true (like old behavior)

sm

protected StringManager sm
The string manager for this package.

started

protected boolean started
Has this component been started yet?

suffix

protected String suffix
The suffix that is added to log file filenames.

writer

protected PrintWriter writer
The PrintWriter to which we are currently logging, if any.

Method Detail

addLifecycleListener

public void addLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.

Parameters: listener The listener to add

backgroundProcess

public void backgroundProcess()
Execute a periodic task, such as reloading, etc. This method will be invoked inside the classloading context of this container. Unexpected throwables will be caught and logged.

createLogElements

protected AccessLogValve.AccessLogElement[] createLogElements()
parse pattern string and create the array of AccessLogElement

findLifecycleListeners

public LifecycleListener[] findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.

getCondition

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

getDirectory

public String getDirectory()
Return the directory in which we create log files.

getEnabled

public boolean getEnabled()

Returns: Returns the enabled.

getFileDateFormat

public String getFileDateFormat()
Return the date format date based log rotation.

getInfo

public String getInfo()
Return descriptive information about this implementation.

getPattern

public String getPattern()
Return the format pattern.

getPrefix

public String getPrefix()
Return the log file prefix.

getSuffix

public String getSuffix()
Return the log file suffix.

invoke

public void invoke(Request request, Response response)
Log a message summarizing the specified request and response, according to the format specified by the pattern property.

Parameters: request Request being processed response Response being processed

Throws: IOException if an input/output error has occurred ServletException if a servlet error has occurred

isBuffered

public boolean isBuffered()
Is the logging buffered

isCheckExists

public boolean isCheckExists()
Check for file existence before logging.

isResolveHosts

public boolean isResolveHosts()
Get the value of the resolve hosts flag.

isRotatable

public boolean isRotatable()
Should we rotate the logs

log

public void log(String message)
Log the specified message to the log file, switching files if the date has changed since the previous log call.

Parameters: message Message to be logged

open

protected void open()
Open the new log file for the date specified by dateStamp.

removeLifecycleListener

public void removeLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.

Parameters: listener The listener to add

rotate

public boolean rotate(String newFileName)
Rename the existing log file to something else. Then open the old log file name up once again. Intended to be called by a JMX agent.

Parameters: newFileName The file name to move the log file entry to

Returns: true if a file was rotated with no error

setBuffered

public void setBuffered(boolean buffered)
Set the value if the logging should be buffered

Parameters: buffered true if buffered.

setCheckExists

public void setCheckExists(boolean checkExists)
Set whether to check for log file existence before logging.

Parameters: checkExists true meaning to check for file existence.

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

setDirectory

public void setDirectory(String directory)
Set the directory in which we create log files.

Parameters: directory The new log file directory

setEnabled

public void setEnabled(boolean enabled)

Parameters: enabled The enabled to set.

setFileDateFormat

public void setFileDateFormat(String fileDateFormat)
Set the date format date based log rotation.

setPattern

public void setPattern(String pattern)
Set the format pattern, first translating any recognized alias.

Parameters: pattern The new pattern

setPrefix

public void setPrefix(String prefix)
Set the log file prefix.

Parameters: prefix The new log file prefix

setResolveHosts

public void setResolveHosts(boolean resolveHosts)
Set the resolve hosts flag.

Parameters: resolveHosts The new resolve hosts value

setRotatable

public void setRotatable(boolean rotatable)
Set the value is we should we rotate the logs

Parameters: rotatable true is we should rotate.

setSuffix

public void setSuffix(String suffix)
Set the log file suffix.

Parameters: suffix The new log file suffix

start

public void start()
Prepare for the beginning of active use of the public methods of this component. This method should be called after configure(), and before any of the public methods of the component are utilized.

Throws: LifecycleException if this component detects a fatal error that prevents this component from being used

stop

public void stop()
Gracefully terminate the active use of the public methods of this component. This method should be the last one called on a given instance of this component.

Throws: LifecycleException if this component detects a fatal error that needs to be reported

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.