Top Description Fields Constructors Methods
org.apache.catalina.valves

public Class AccessLogValve

extends AbstractAccessLogValve
Class Inheritance
Known Direct Subclasses
org.apache.catalina.valves.ExtendedAccessLogValve, org.apache.catalina.valves.JsonAccessLogValve
Imports
java.io.BufferedWriter, .CharArrayWriter, .File, .FileOutputStream, .IOException, .OutputStreamWriter, .PrintWriter, .UnsupportedEncodingException, java.nio.charset.Charset, .StandardCharsets, java.text.SimpleDateFormat, java.util.Date, .Locale, .TimeZone, org.apache.catalina.LifecycleException, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.util.ExceptionUtils, org.apache.tomcat.util.buf.B2CConverter

This is a concrete implementation of AbstractAccessLogValve that outputs the access log to a file. The features of this implementation include:

For UNIX users, another field called checkExists is also available. If set to true, the log file's existence will be checked before each logging. This way an external log rotator can move the file somewhere and Tomcat will start with a new file.

For JMX junkies, a public method called rotate has been made available to allow you to tell this instance to move the existing log file to somewhere else and start writing a new log file.

Field Summary

Modifier and TypeField and Description
private boolean
buffered

Buffered logging.

private boolean
checkExists

Do we check for log file existence?

private volatile boolean
protected File
currentLogFile

The current log file we are writing to.

private volatile String
dateStamp

The as-of date for the currently open log file, or a zero-length string if there is no open log file.

private String
directory

The directory in which log files are created.

protected volatile String
encoding

Character set used by the log file.

protected String
fileDateFormat

Date format to place in log file name.

protected SimpleDateFormat
fileDateFormatter

A date formatter to format a Date using the format given by fileDateFormat.

private static final Log
private int
maxDays

The number of days to retain the access log files before they are removed.

protected volatile String
prefix

The prefix that is added to log file filenames.

protected boolean
renameOnRotate

Should we defer inclusion of the date stamp in the file name until rotate time?

protected boolean
rotatable

Should we rotate our log file?

private volatile long
rotationLastChecked

Instant when the log daily rotation was last checked.

protected volatile String
suffix

The suffix that is added to log file filenames.

protected PrintWriter
writer

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

Inherited from org.apache.catalina.valves.AbstractAccessLogValve:
cachedElementsconditionconditionIfenabledlocalelocaleNamelogElementspatternrequestAttributesEnabled

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public synchronized void
backgroundProcess()

Overrides org.apache.catalina.valves.ValveBase.backgroundProcess.

Implements org.apache.catalina.Valve.backgroundProcess.

Execute a periodic task, such as reloading, etc. This method will be invoked inside the classloading context of this container.

private synchronized void
close(boolean
Rename file to final name after closing
rename
)

Close the currently open log file (if any)

public String

Returns:

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

private File
public String

Returns:

Character set name, or null if the default character set is used.
getEncoding
()

Return the character set name that is used to write the log file.

public String

Returns:

the date format date based log rotation.
getFileDateFormat
()

private File

Returns:

the log file object
getLogFile
(boolean
include the timestamp in the file name.
useDateStamp
)

Create a File object based on the current log file name.

public int
public String

Returns:

the log file prefix.
getPrefix
()

public String

Returns:

the log file suffix.
getSuffix
()

public boolean

Returns:

true if the logging uses a buffer
isBuffered
()

Is the logging buffered.

public boolean

Returns:

true if file existence is checked first
isCheckExists
()

Check for file existence before logging.

public boolean

Returns:

true if the logs file names are time stamped only when they are rotated
isRenameOnRotate
()

Should we defer inclusion of the date stamp in the file name until rotate time.

public boolean

Returns:

true if the access log should be rotated
isRotatable
()

Should we rotate the access log.

public void
log(CharArrayWriter
Message to be logged
message
)

Implements abstract org.apache.catalina.valves.AbstractAccessLogValve.log.

Log the specified message to the log file, switching files if the date has changed since the previous log call.

protected synchronized void
open()

Open the new log file for the date specified by dateStamp.

private void
restore()

Move a current but rotated log file back to the unrotated one.

public void
rotate()

Rotate the log file if necessary.

public synchronized boolean

Returns:

true if a file was rotated with no error
rotate
(String
The file name to move the log file entry to
newFileName
)

Rename the existing log file to something else.

public void
setBuffered(boolean
true if buffered.
buffered
)

Set the value if the logging should be buffered

public void
setCheckExists(boolean
true meaning to check for file existence.
checkExists
)

Set whether to check for log file existence before logging.

public void
setDirectory(String
The new log file directory
directory
)

Set the directory in which we create log files.

public void
setEncoding(String
The name of the character set.
encoding
)

Set the character set that is used to write the log file.

public void
setFileDateFormat(String
The format for the file timestamp
fileDateFormat
)

Set the date format date based log rotation.

public void
setMaxDays(int maxDays)

public void
setPrefix(String
The new log file prefix
prefix
)

Set the log file prefix.

public void
setRenameOnRotate(boolean
true if defer inclusion of date stamp
renameOnRotate
)

Set the value if we should defer inclusion of the date stamp in the file name until rotate time

public void
setRotatable(boolean
true if the log should be rotated
rotatable
)

Configure whether the access log should be rotated.

public void
setSuffix(String
The new log file suffix
suffix
)

Set the log file suffix.

protected void
protected void
Inherited from org.apache.catalina.valves.AbstractAccessLogValve:
createAccessLogElementcreateAccessLogElementcreateLogElementsescapeAndAppendfindLocalegetConditiongetConditionIfgetConditionUnlessgetEnabledgetIpv6CanonicalgetLocalegetMaxLogMessageBufferSizegetPatterngetRequestAttributesEnabledinvokelogsetConditionsetConditionIfsetConditionUnlesssetEnabledsetIpv6CanonicalsetLocalesetMaxLogMessageBufferSizesetPatternsetRequestAttributesEnabled

Field Detail

bufferedback to summary
private boolean buffered

Buffered logging.

checkExistsback to summary
private boolean checkExists

Do we check for log file existence? Helpful if an external agent renames the log file so we can automagically recreate it.

checkForOldLogsback to summary
private volatile boolean checkForOldLogs
currentLogFileback to summary
protected File currentLogFile

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

dateStampback to summary
private volatile String dateStamp

The as-of date for the currently open log file, or a zero-length string if there is no open log file.

directoryback to summary
private String directory

The directory in which log files are created.

encodingback to summary
protected volatile String encoding

Character set used by the log file. If it is null, UTF-8 will be used. An empty string will be treated as null when this property is assigned.

fileDateFormatback to summary
protected String fileDateFormat

Date format to place in log file name.

fileDateFormatterback to summary
protected SimpleDateFormat fileDateFormatter

A date formatter to format a Date using the format given by fileDateFormat.

logback to summary
private static final Log log

Hides org.apache.catalina.valves.AbstractAccessLogValve.log.

maxDaysback to summary
private int maxDays

The number of days to retain the access log files before they are removed.

prefixback to summary
protected volatile String prefix

The prefix that is added to log file filenames.

renameOnRotateback to summary
protected boolean renameOnRotate

Should we defer inclusion of the date stamp in the file name until rotate time? Default is false.

rotatableback to summary
protected boolean rotatable

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

rotationLastCheckedback to summary
private volatile long rotationLastChecked

Instant when the log daily rotation was last checked.

suffixback to summary
protected volatile String suffix

The suffix that is added to log file filenames.

writerback to summary
protected PrintWriter writer

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

Constructor Detail

AccessLogValveback to summary
public AccessLogValve()

Method Detail

backgroundProcessback to summary
public synchronized void backgroundProcess()

Overrides org.apache.catalina.valves.ValveBase.backgroundProcess.

Implements org.apache.catalina.Valve.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.

Annotations
@Override
closeback to summary
private synchronized void close(boolean rename)

Close the currently open log file (if any)

Parameters
rename:boolean

Rename file to final name after closing

getDirectoryback to summary
public String getDirectory()
Returns:String

the directory in which we create log files.

getDirectoryFileback to summary
private File getDirectoryFile()
getEncodingback to summary
public String getEncoding()

Return the character set name that is used to write the log file.

Returns:String

Character set name, or null if the default character set is used.

getFileDateFormatback to summary
public String getFileDateFormat()
Returns:String

the date format date based log rotation.

getLogFileback to summary
private File getLogFile(boolean useDateStamp)

Create a File object based on the current log file name. Directories are created as needed but the underlying file is not created or opened.

Parameters
useDateStamp:boolean

include the timestamp in the file name.

Returns:File

the log file object

getMaxDaysback to summary
public int getMaxDays()
getPrefixback to summary
public String getPrefix()
Returns:String

the log file prefix.

getSuffixback to summary
public String getSuffix()
Returns:String

the log file suffix.

isBufferedback to summary
public boolean isBuffered()

Is the logging buffered. Usually buffering can increase performance.

Returns:boolean

true if the logging uses a buffer

isCheckExistsback to summary
public boolean isCheckExists()

Check for file existence before logging.

Returns:boolean

true if file existence is checked first

isRenameOnRotateback to summary
public boolean isRenameOnRotate()

Should we defer inclusion of the date stamp in the file name until rotate time.

Returns:boolean

true if the logs file names are time stamped only when they are rotated

isRotatableback to summary
public boolean isRotatable()

Should we rotate the access log.

Returns:boolean

true if the access log should be rotated

logback to summary
public void log(CharArrayWriter message)

Implements abstract org.apache.catalina.valves.AbstractAccessLogValve.log.

Log the specified message to the log file, switching files if the date has changed since the previous log call.

Parameters
message:CharArrayWriter

Message to be logged

Annotations
@Override
openback to summary
protected synchronized void open()

Open the new log file for the date specified by dateStamp.

restoreback to summary
private void restore()

Move a current but rotated log file back to the unrotated one. Needed if date stamp inclusion is deferred to rotation time.

rotateback to summary
public void rotate()

Rotate the log file if necessary.

rotateback to summary
public synchronized 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:String

The file name to move the log file entry to

Returns:boolean

true if a file was rotated with no error

setBufferedback to summary
public void setBuffered(boolean buffered)

Set the value if the logging should be buffered

Parameters
buffered:boolean

true if buffered.

setCheckExistsback to summary
public void setCheckExists(boolean checkExists)

Set whether to check for log file existence before logging.

Parameters
checkExists:boolean

true meaning to check for file existence.

setDirectoryback to summary
public void setDirectory(String directory)

Set the directory in which we create log files.

Parameters
directory:String

The new log file directory

setEncodingback to summary
public void setEncoding(String encoding)

Set the character set that is used to write the log file.

Parameters
encoding:String

The name of the character set.

setFileDateFormatback to summary
public void setFileDateFormat(String fileDateFormat)

Set the date format date based log rotation.

Parameters
fileDateFormat:String

The format for the file timestamp

setMaxDaysback to summary
public void setMaxDays(int maxDays)
setPrefixback to summary
public void setPrefix(String prefix)

Set the log file prefix.

Parameters
prefix:String

The new log file prefix

setRenameOnRotateback to summary
public void setRenameOnRotate(boolean renameOnRotate)

Set the value if we should defer inclusion of the date stamp in the file name until rotate time

Parameters
renameOnRotate:boolean

true if defer inclusion of date stamp

setRotatableback to summary
public void setRotatable(boolean rotatable)

Configure whether the access log should be rotated.

Parameters
rotatable:boolean

true if the log should be rotated

setSuffixback to summary
public void setSuffix(String suffix)

Set the log file suffix.

Parameters
suffix:String

The new log file suffix

startInternalback to summary
protected void startInternal() throws LifecycleException

Overrides org.apache.catalina.valves.ValveBase.startInternal.

Start this component and implement the requirements of org.apache.catalina.util.LifecycleBase#startInternal().

Annotations
@Override
Exceptions
LifecycleException:
if this component detects a fatal error that prevents this component from being used
stopInternalback to summary
protected void stopInternal() throws LifecycleException

Overrides org.apache.catalina.valves.ValveBase.stopInternal.

Stop this component and implement the requirements of org.apache.catalina.util.LifecycleBase#stopInternal().

Annotations
@Override
Exceptions
LifecycleException:
if this component detects a fatal error that prevents this component from being used