Class RollingFileAppender
- java.lang.Object
-
- org.apache.log4j.AppenderSkeleton
-
- org.apache.log4j.WriterAppender
-
- org.apache.log4j.FileAppender
-
- org.apache.log4j.rolling.RollingFileAppender
-
- All Implemented Interfaces:
org.apache.log4j.Appender
,org.apache.log4j.spi.OptionHandler
,org.apache.log4j.xml.UnrecognizedElementHandler
public final class RollingFileAppender extends org.apache.log4j.FileAppender implements org.apache.log4j.xml.UnrecognizedElementHandler
RollingFileAppender
extendsFileAppender
to backup the log files depending onRollingPolicy
andTriggeringPolicy
.To be of any use, a
RollingFileAppender
instance must have both aRollingPolicy
and aTriggeringPolicy
set up. However, if itsRollingPolicy
also implements theTriggeringPolicy
interface, then only the former needs to be set up. For example,TimeBasedRollingPolicy
acts both as aRollingPolicy
and aTriggeringPolicy
.RollingFileAppender
can be configured programattically or usingDOMConfigurator
orDOMConfigurator
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 Classes Modifier and Type Class Description private static class
RollingFileAppender.CountingOutputStream
Wrapper for OutputStream that will report all write operations back to this class for file length calculations.private static class
RollingFileAppender.DefaultErrorHandler
-
Field Summary
Fields Modifier and Type Field Description private long
fileLength
Length of current active log file.private Action
lastRolloverAsyncAction
Asynchronous action (like compression) from previous rollover.private RollingPolicy
rollingPolicy
Rolling policy.private TriggeringPolicy
triggeringPolicy
Triggering policy.
-
Constructor Summary
Constructors Constructor Description RollingFileAppender()
Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activateOptions()
Prepare instance of use.void
close()
Close appender.private java.io.FileOutputStream
createFileOutputStream(java.lang.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.QuietWriter
createQuietWriter(java.io.Writer writer)
protected java.io.OutputStreamWriter
createWriter(java.io.OutputStream os)
Returns an OutputStreamWriter when passed an OutputStream.long
getFileLength()
Get byte length of current active log file.RollingPolicy
getRollingPolicy()
Get rolling policy.TriggeringPolicy
getTriggeringPolicy()
Get triggering policy.void
incrementFileLength(int increment)
Increments estimated byte length of current active log file.boolean
parseUnrecognizedElement(org.w3c.dom.Element element, java.util.Properties props)
boolean
rollover()
Implements the usual roll over behaviour.void
setRollingPolicy(RollingPolicy policy)
Sets the rolling policy.void
setTriggeringPolicy(TriggeringPolicy policy)
Set triggering policy.protected void
subAppend(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, setQWForFiles
-
Methods inherited from class org.apache.log4j.WriterAppender
append, checkEntryConditions, closeWriter, getEncoding, getImmediateFlush, requiresLayout, setEncoding, setErrorHandler, setImmediateFlush, setWriter, shouldFlush, writeFooter, writeHeader
-
-
-
-
Field Detail
-
triggeringPolicy
private TriggeringPolicy triggeringPolicy
Triggering policy.
-
rollingPolicy
private RollingPolicy rollingPolicy
Rolling policy.
-
fileLength
private long fileLength
Length of current active log file.
-
lastRolloverAsyncAction
private Action lastRolloverAsyncAction
Asynchronous action (like compression) from previous rollover.
-
-
Method Detail
-
activateOptions
public void activateOptions()
Prepare instance of use.- Specified by:
activateOptions
in interfaceorg.apache.log4j.spi.OptionHandler
- Overrides:
activateOptions
in classorg.apache.log4j.FileAppender
-
createQuietWriter
private org.apache.log4j.helpers.QuietWriter createQuietWriter(java.io.Writer writer)
-
rollover
public boolean rollover()
Implements the usual roll over behaviour.If
MaxBackupIndex
is positive, then files {File.1
, ...,File.MaxBackupIndex -1
} are renamed to {File.2
, ...,File.MaxBackupIndex
}. Moreover,File
is renamedFile.1
and closed. A newFile
is created to receive further log output.If
MaxBackupIndex
is equal to zero, then theFile
is truncated with no backup files created.- Returns:
- true if rollover performed.
-
createFileOutputStream
private java.io.FileOutputStream createFileOutputStream(java.lang.String newFileName, boolean append) throws java.io.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:
java.io.FileNotFoundException
- if creating log file or parent directories was unsuccessful
-
subAppend
protected void subAppend(org.apache.log4j.spi.LoggingEvent event)
- Overrides:
subAppend
in classorg.apache.log4j.WriterAppender
-
getRollingPolicy
public RollingPolicy getRollingPolicy()
Get rolling policy.- Returns:
- rolling policy.
-
getTriggeringPolicy
public TriggeringPolicy getTriggeringPolicy()
Get triggering policy.- Returns:
- triggering policy.
-
setRollingPolicy
public void setRollingPolicy(RollingPolicy policy)
Sets the rolling policy.- Parameters:
policy
- rolling policy.
-
setTriggeringPolicy
public void setTriggeringPolicy(TriggeringPolicy policy)
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:
close
in interfaceorg.apache.log4j.Appender
- Overrides:
close
in classorg.apache.log4j.WriterAppender
-
createWriter
protected java.io.OutputStreamWriter createWriter(java.io.OutputStream os)
Returns an OutputStreamWriter when passed an OutputStream. The encoding used will depend on the value of theencoding
property. 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:
createWriter
in 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
public boolean parseUnrecognizedElement(org.w3c.dom.Element element, java.util.Properties props) throws java.lang.Exception
- Specified by:
parseUnrecognizedElement
in interfaceorg.apache.log4j.xml.UnrecognizedElementHandler
- Throws:
java.lang.Exception
-
-