Class RollingFileAppender
- All Implemented Interfaces:
org.apache.log4j.Appender, org.apache.log4j.spi.OptionHandler, org.apache.log4j.xml.UnrecognizedElementHandler
RollingFileAppender extends FileAppender to backup the log files
depending on RollingPolicy and TriggeringPolicy.
To be of any use, a RollingFileAppender instance must have both
a RollingPolicy and a TriggeringPolicy set up.
However, if its RollingPolicy also implements the
TriggeringPolicy interface, then only the former needs to be
set up. For example, TimeBasedRollingPolicy acts both as a
RollingPolicy and a TriggeringPolicy.
RollingFileAppender can be configured programattically or
using DOMConfigurator or
DOMConfigurator in log4j 1.2.15 or later. Here is a sample
configration file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration>
<log4j:configuration debug="true">
<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="/wombat/foo.%d{yyyy-MM}.gz"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c{1} - %m%n"/>
</layout>
</appender>
<root">
<appender-ref ref="ROLL"/>
</root>
</log4j:configuration>
This configuration file specifies a monthly rollover schedule including
automatic compression of the archived files. See
TimeBasedRollingPolicy for more details.
- Since:
- 1.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classWrapper for OutputStream that will report all write operations back to this class for file length calculations.private static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate longLength of current active log file.private ActionAsynchronous action (like compression) from previous rollover.private RollingPolicyRolling policy.private TriggeringPolicyTriggering policy.Fields inherited from class org.apache.log4j.FileAppender
bufferedIO, bufferSize, fileAppend, fileNameFields inherited from class org.apache.log4j.WriterAppender
encoding, immediateFlush, qwFields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidPrepare instance of use.voidclose()Close appender.private FileOutputStreamcreateFileOutputStream(String newFileName, boolean append) Creates a new FileOutputStream of a new log file, possibly creating first all the needed parent directoriesprivate org.apache.log4j.helpers.QuietWritercreateQuietWriter(Writer writer) protected OutputStreamWriterReturns an OutputStreamWriter when passed an OutputStream.longGet byte length of current active log file.Get rolling policy.Get triggering policy.voidincrementFileLength(int increment) Increments estimated byte length of current active log file.booleanparseUnrecognizedElement(Element element, Properties props) booleanrollover()Implements the usual roll over behaviour.voidsetRollingPolicy(RollingPolicy policy) Sets the rolling policy.voidsetTriggeringPolicy(TriggeringPolicy policy) Set triggering policy.protected voidsubAppend(org.apache.log4j.spi.LoggingEvent event) Methods inherited from class org.apache.log4j.FileAppender
closeFile, getAppend, getBufferedIO, getBufferSize, getFile, reset, setAppend, setBufferedIO, setBufferSize, setFile, setFile, setQWForFilesMethods inherited from class org.apache.log4j.WriterAppender
append, checkEntryConditions, closeWriter, getEncoding, getImmediateFlush, requiresLayout, setEncoding, setErrorHandler, setImmediateFlush, setWriter, shouldFlush, writeFooter, writeHeaderMethods inherited from class org.apache.log4j.AppenderSkeleton
addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setLayout, setName, setThreshold
-
Field Details
-
triggeringPolicy
Triggering policy. -
rollingPolicy
Rolling policy. -
fileLength
private long fileLengthLength of current active log file. -
lastRolloverAsyncAction
Asynchronous action (like compression) from previous rollover.
-
-
Constructor Details
-
RollingFileAppender
public RollingFileAppender()Construct a new instance.
-
-
Method Details
-
activateOptions
public void activateOptions()Prepare instance of use.- Specified by:
activateOptionsin interfaceorg.apache.log4j.spi.OptionHandler- Overrides:
activateOptionsin classorg.apache.log4j.FileAppender
-
createQuietWriter
-
rollover
public boolean rollover()Implements the usual roll over behaviour.If
MaxBackupIndexis positive, then files {File.1, ...,File.MaxBackupIndex -1} are renamed to {File.2, ...,File.MaxBackupIndex}. Moreover,Fileis renamedFile.1and closed. A newFileis created to receive further log output.If
MaxBackupIndexis equal to zero, then theFileis truncated with no backup files created.- Returns:
- true if rollover performed.
-
createFileOutputStream
private FileOutputStream createFileOutputStream(String newFileName, boolean append) throws FileNotFoundException Creates a new FileOutputStream of a new log file, possibly creating first all the needed parent directories- Parameters:
newFileName- Filename of new log file to be createdappend- If file should be appended- Returns:
- newly created FileOutputStream
- Throws:
FileNotFoundException- if creating log file or parent directories was unsuccessful
-
subAppend
protected void subAppend(org.apache.log4j.spi.LoggingEvent event) - Overrides:
subAppendin classorg.apache.log4j.WriterAppender
-
getRollingPolicy
-
getTriggeringPolicy
-
setRollingPolicy
Sets the rolling policy.- Parameters:
policy- rolling policy.
-
setTriggeringPolicy
Set triggering policy.- Parameters:
policy- triggering policy.
-
close
public void close()Close appender. Waits for any asynchronous file compression actions to be completed.- Specified by:
closein interfaceorg.apache.log4j.Appender- Overrides:
closein classorg.apache.log4j.WriterAppender
-
createWriter
Returns an OutputStreamWriter when passed an OutputStream. The encoding used will depend on the value of theencodingproperty. If the encoding value is specified incorrectly the writer will be opened using the default system encoding (an error message will be printed to the loglog.- Overrides:
createWriterin classorg.apache.log4j.WriterAppender- Parameters:
os- output stream, may not be null.- Returns:
- new writer.
-
getFileLength
public long getFileLength()Get byte length of current active log file.- Returns:
- byte length of current active log file.
-
incrementFileLength
public void incrementFileLength(int increment) Increments estimated byte length of current active log file.- Parameters:
increment- additional bytes written to log file.
-
parseUnrecognizedElement
- Specified by:
parseUnrecognizedElementin interfaceorg.apache.log4j.xml.UnrecognizedElementHandler- Throws:
Exception
-