Class DefaultRolloverStrategy
- java.lang.Object
-
- org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy
-
- org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy
-
- All Implemented Interfaces:
RolloverStrategy
@Plugin(name="DefaultRolloverStrategy", category="Core", printObject=true) public class DefaultRolloverStrategy extends AbstractRolloverStrategy
When rolling over,DefaultRolloverStrategy
renames files according to an algorithm as described below.The DefaultRolloverStrategy is a combination of a time-based policy and a fixed-window policy. When the file name pattern contains a date format then the rollover time interval will be used to calculate the time to use in the file pattern. When the file pattern contains an integer replacement token one of the counting techniques will be used.
When the ascending attribute is set to true (the default) then the counter will be incremented and the current log file will be renamed to include the counter value. If the counter hits the maximum value then the oldest file, which will have the smallest counter, will be deleted, all other files will be renamed to have their counter decremented and then the current file will be renamed to have the maximum counter value. Note that with this counting strategy specifying a large maximum value may entirely avoid renaming files.
When the ascending attribute is false, then the "normal" fixed-window strategy will be used.
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DefaultRolloverStrategy.Builder
Builds DefaultRolloverStrategy instances.
-
Field Summary
Fields Modifier and Type Field Description private int
compressionLevel
private java.util.List<Action>
customActions
private static int
DEFAULT_WINDOW_SIZE
private int
maxIndex
Index for oldest retained log file.private static int
MIN_WINDOW_SIZE
private int
minIndex
Index for most recent log file.private boolean
stopCustomActionsOnError
private PatternProcessor
tempCompressedFilePattern
private boolean
useMax
-
Fields inherited from class org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy
LOGGER, PATTERN_COUNTER, strSubstitutor
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DefaultRolloverStrategy(int minIndex, int maxIndex, boolean useMax, int compressionLevel, StrSubstitutor strSubstitutor, Action[] customActions, boolean stopCustomActionsOnError)
Deprecated.Since 2.9 Added tempCompressedFilePatternString parameterprotected
DefaultRolloverStrategy(int minIndex, int maxIndex, boolean useMax, int compressionLevel, StrSubstitutor strSubstitutor, Action[] customActions, boolean stopCustomActionsOnError, java.lang.String tempCompressedFilePatternString)
Constructs a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static DefaultRolloverStrategy
createStrategy(java.lang.String max, java.lang.String min, java.lang.String fileIndex, java.lang.String compressionLevelStr, Action[] customActions, boolean stopCustomActionsOnError, Configuration config)
Deprecated.Since 2.9 Usage of Builder API is preferableint
getCompressionLevel()
java.util.List<Action>
getCustomActions()
int
getMaxIndex()
int
getMinIndex()
PatternProcessor
getTempCompressedFilePattern()
boolean
isStopCustomActionsOnError()
boolean
isUseMax()
static DefaultRolloverStrategy.Builder
newBuilder()
private int
purge(int lowIndex, int highIndex, RollingFileManager manager)
private int
purgeAscending(int lowIndex, int highIndex, RollingFileManager manager)
Purges and renames old log files in preparation for rollover.private int
purgeDescending(int lowIndex, int highIndex, RollingFileManager manager)
Purges and renames old log files in preparation for rollover.RolloverDescription
rollover(RollingFileManager manager)
Performs the rollover.java.lang.String
toString()
-
Methods inherited from class org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy
getEligibleFiles, getEligibleFiles, getEligibleFiles, getEligibleFiles, getEligibleFiles, getStrSubstitutor, merge, suffixLength
-
-
-
-
Field Detail
-
MIN_WINDOW_SIZE
private static final int MIN_WINDOW_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_WINDOW_SIZE
private static final int DEFAULT_WINDOW_SIZE
- See Also:
- Constant Field Values
-
maxIndex
private final int maxIndex
Index for oldest retained log file.
-
minIndex
private final int minIndex
Index for most recent log file.
-
useMax
private final boolean useMax
-
compressionLevel
private final int compressionLevel
-
customActions
private final java.util.List<Action> customActions
-
stopCustomActionsOnError
private final boolean stopCustomActionsOnError
-
tempCompressedFilePattern
private final PatternProcessor tempCompressedFilePattern
-
-
Constructor Detail
-
DefaultRolloverStrategy
@Deprecated protected DefaultRolloverStrategy(int minIndex, int maxIndex, boolean useMax, int compressionLevel, StrSubstitutor strSubstitutor, Action[] customActions, boolean stopCustomActionsOnError)
Deprecated.Since 2.9 Added tempCompressedFilePatternString parameterConstructs a new instance.- Parameters:
minIndex
- The minimum index.maxIndex
- The maximum index.customActions
- custom actions to perform asynchronously after rolloverstopCustomActionsOnError
- whether to stop executing asynchronous actions if an error occurs
-
DefaultRolloverStrategy
protected DefaultRolloverStrategy(int minIndex, int maxIndex, boolean useMax, int compressionLevel, StrSubstitutor strSubstitutor, Action[] customActions, boolean stopCustomActionsOnError, java.lang.String tempCompressedFilePatternString)
Constructs a new instance.- Parameters:
minIndex
- The minimum index.maxIndex
- The maximum index.customActions
- custom actions to perform asynchronously after rolloverstopCustomActionsOnError
- whether to stop executing asynchronous actions if an error occurstempCompressedFilePatternString
- File pattern of the working file used during compression, if null no temporary file are used
-
-
Method Detail
-
newBuilder
@PluginBuilderFactory public static DefaultRolloverStrategy.Builder newBuilder()
-
createStrategy
@PluginFactory @Deprecated public static DefaultRolloverStrategy createStrategy(@PluginAttribute("max") java.lang.String max, @PluginAttribute("min") java.lang.String min, @PluginAttribute("fileIndex") java.lang.String fileIndex, @PluginAttribute("compressionLevel") java.lang.String compressionLevelStr, @PluginElement("Actions") Action[] customActions, @PluginAttribute(value="stopCustomActionsOnError",defaultBoolean=true) boolean stopCustomActionsOnError, @PluginConfiguration Configuration config)
Deprecated.Since 2.9 Usage of Builder API is preferableCreates the DefaultRolloverStrategy.- Parameters:
max
- The maximum number of files to keep.min
- The minimum number of files to keep.fileIndex
- If set to "max" (the default), files with a higher index will be newer than files with a smaller index. If set to "min", file renaming and the counter will follow the Fixed Window strategy.compressionLevelStr
- The compression level, 0 (less) through 9 (more); applies only to ZIP files.customActions
- custom actions to perform asynchronously after rolloverstopCustomActionsOnError
- whether to stop executing asynchronous actions if an error occursconfig
- The Configuration.- Returns:
- A DefaultRolloverStrategy.
-
getCompressionLevel
public int getCompressionLevel()
-
getCustomActions
public java.util.List<Action> getCustomActions()
-
getMaxIndex
public int getMaxIndex()
-
getMinIndex
public int getMinIndex()
-
isStopCustomActionsOnError
public boolean isStopCustomActionsOnError()
-
isUseMax
public boolean isUseMax()
-
getTempCompressedFilePattern
public PatternProcessor getTempCompressedFilePattern()
-
purge
private int purge(int lowIndex, int highIndex, RollingFileManager manager)
-
purgeAscending
private int purgeAscending(int lowIndex, int highIndex, RollingFileManager manager)
Purges and renames old log files in preparation for rollover. The oldest file will have the smallest index, the newest the highest.- Parameters:
lowIndex
- low index. Log file associated with low index will be deleted if needed.highIndex
- high index.manager
- The RollingFileManager- Returns:
- true if purge was successful and rollover should be attempted.
-
purgeDescending
private int purgeDescending(int lowIndex, int highIndex, RollingFileManager manager)
Purges and renames old log files in preparation for rollover. The newest file will have the smallest index, the oldest will have the highest.- Parameters:
lowIndex
- low indexhighIndex
- high index. Log file associated with high index will be deleted if needed.manager
- The RollingFileManager- Returns:
- true if purge was successful and rollover should be attempted.
-
rollover
public RolloverDescription rollover(RollingFileManager manager) throws java.lang.SecurityException
Performs the rollover.- Parameters:
manager
- The RollingFileManager name for current active log file.- Returns:
- A RolloverDescription.
- Throws:
java.lang.SecurityException
- if an error occurs.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-