Class FixedWindowRollingPolicy
- java.lang.Object
-
- org.apache.log4j.rolling.RollingPolicyBase
-
- org.apache.log4j.rolling.FixedWindowRollingPolicy
-
- All Implemented Interfaces:
RollingPolicy
,org.apache.log4j.spi.OptionHandler
public final class FixedWindowRollingPolicy extends RollingPolicyBase
When rolling over,FixedWindowRollingPolicy
renames files according to a fixed window algorithm as described below.The ActiveFileName property, which is required, represents the name of the file where current logging output will be written. The FileNamePattern option represents the file name pattern for the archived (rolled over) log files. If present, the FileNamePattern option must include an integer token, that is the string "%i" somewhere within the pattern.
Let max and min represent the values of respectively the MaxIndex and MinIndex options. Let "foo.log" be the value of the ActiveFile option and "foo.%i.log" the value of FileNamePattern. Then, when rolling over, the file
foo.max.log
will be deleted, the filefoo.max-1.log
will be renamed asfoo.max.log
, the filefoo.max-2.log
renamed asfoo.max-1.log
, and so on, the filefoo.min+1.log
renamed asfoo.min+2.log
. Lastly, the active filefoo.log
will be renamed asfoo.min.log
and a new active file namefoo.log
will be created.Given that this rollover algorithm requires as many file renaming operations as the window size, large window sizes are discouraged. The current implementation will automatically reduce the window size to 12 when larger values are specified by the user.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
explicitActiveFile
if true, then an explicit name for the active file was specified using RollingFileAppender.file or the redundent RollingPolicyBase.setActiveFileprivate static int
MAX_WINDOW_SIZE
It's almost always a bad idea to have a large window size, say over 12.private int
maxIndex
Index for oldest retained log file.private int
minIndex
Index for most recent log file.-
Fields inherited from class org.apache.log4j.rolling.RollingPolicyBase
activeFileName
-
-
Constructor Summary
Constructors Constructor Description FixedWindowRollingPolicy()
Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activateOptions()
int
getMaxIndex()
Get index of oldest log file to be retained.int
getMinIndex()
Get index of most recent log file.RolloverDescription
initialize(java.lang.String file, boolean append)
Initialize the policy and return any initial actions for rolling file appender..private boolean
purge(int lowIndex, int highIndex)
Purge and rename old log files in preparation for rolloverRolloverDescription
rollover(java.lang.String currentFileName)
Prepare for a rollover.void
setMaxIndex(int maxIndex)
Set index of oldest log file to be retained.void
setMinIndex(int minIndex)
Set index of most recent log file.-
Methods inherited from class org.apache.log4j.rolling.RollingPolicyBase
formatFileName, getActiveFileName, getDatePatternConverter, getFileNamePattern, getIntegerPatternConverter, parseFileNamePattern, setActiveFileName, setFileNamePattern
-
-
-
-
Field Detail
-
MAX_WINDOW_SIZE
private static final int MAX_WINDOW_SIZE
It's almost always a bad idea to have a large window size, say over 12.- See Also:
- Constant Field Values
-
maxIndex
private int maxIndex
Index for oldest retained log file.
-
minIndex
private int minIndex
Index for most recent log file.
-
explicitActiveFile
private boolean explicitActiveFile
if true, then an explicit name for the active file was specified using RollingFileAppender.file or the redundent RollingPolicyBase.setActiveFile
-
-
Method Detail
-
activateOptions
public void activateOptions()
- Specified by:
activateOptions
in interfaceorg.apache.log4j.spi.OptionHandler
- Overrides:
activateOptions
in classRollingPolicyBase
-
initialize
public RolloverDescription initialize(java.lang.String file, boolean append)
Initialize the policy and return any initial actions for rolling file appender..- Parameters:
file
- current value of RollingFileAppender.getFile().append
- current value of RollingFileAppender.getAppend().- Returns:
- Description of the initialization, may be null to indicate no initialization needed.
-
rollover
public RolloverDescription rollover(java.lang.String currentFileName)
Prepare for a rollover. This method is called prior to closing the active log file, performs any necessary preliminary actions and describes actions needed after close of current log file.- Parameters:
currentFileName
- file name for current active log file.- Returns:
- Description of pending rollover, may be null to indicate no rollover at this time.
-
getMaxIndex
public int getMaxIndex()
Get index of oldest log file to be retained.- Returns:
- index of oldest log file.
-
getMinIndex
public int getMinIndex()
Get index of most recent log file.- Returns:
- index of oldest log file.
-
setMaxIndex
public void setMaxIndex(int maxIndex)
Set index of oldest log file to be retained.- Parameters:
maxIndex
- index of oldest log file to be retained.
-
setMinIndex
public void setMinIndex(int minIndex)
Set index of most recent log file.- Parameters:
minIndex
- Index of most recent log file.
-
purge
private boolean purge(int lowIndex, int highIndex)
Purge and rename old log files in preparation for rollover- Parameters:
lowIndex
- low indexhighIndex
- high index. Log file associated with high index will be deleted if needed.- Returns:
- true if purge was successful and rollover should be attempted.
-
-