Class TimeBasedRollingPolicy
- java.lang.Object
-
- org.apache.log4j.rolling.RollingPolicyBase
-
- org.apache.log4j.rolling.TimeBasedRollingPolicy
-
- All Implemented Interfaces:
RollingPolicy,TriggeringPolicy,org.apache.log4j.spi.OptionHandler
public final class TimeBasedRollingPolicy extends RollingPolicyBase implements TriggeringPolicy
TimeBasedRollingPolicyis both easy to configure and quite powerful.In order to use
TimeBasedRollingPolicy, the FileNamePattern option must be set. It basically specifies the name of the rolled log files. The valueFileNamePatternshould consist of the name of the file, plus a suitably placed%dconversion specifier. The%dconversion specifier may contain a date and time pattern as specified by theSimpleDateFormatclass. If the date and time pattern is ommitted, then the default pattern of "yyyy-MM-dd" is assumed. The following examples should clarify the point.FileNamePatternvalueRollover schedule Example /wombat/folder/foo.%dDaily rollover (at midnight). Due to the omission of the optional time and date pattern for the %d token specifier, the default pattern of "yyyy-MM-dd" is assumed, which corresponds to daily rollover. During November 23rd, 2004, logging output will go to the file /wombat/foo.2004-11-23. At midnight and for the rest of the 24th, logging output will be directed to/wombat/foo.2004-11-24./wombat/foo.%d{yyyy-MM}.logRollover at the beginning of each month. During the month of October 2004, logging output will go to /wombat/foo.2004-10.log. After midnight of October 31st and for the rest of November, logging output will be directed to/wombat/foo.2004-11.log.Automatic file compression
TimeBasedRollingPolicysupports automatic file compression. This feature is enabled if the value of the FileNamePattern option ends with.gzor.zip.FileNamePatternvalueRollover schedule Example /wombat/foo.%d.gzDaily rollover (at midnight) with automatic GZIP compression of the archived files. During November 23rd, 2004, logging output will go to the file /wombat/foo.2004-11-23. However, at midnight that file will be compressed to become/wombat/foo.2004-11-23.gz. For the 24th of November, logging output will be directed to/wombat/folder/foo.2004-11-24until its rolled over at the beginning of the next day.Decoupling the location of the active log file and the archived log files
The active file is defined as the log file for the current period whereas archived files are those files which have been rolled over in previous periods.
By setting the ActiveFileName option you can decouple the location of the active log file and the location of the archived log files.
FileNamePatternvalueActiveFileName Rollover schedule Example /wombat/foo.log.%d/wombat/foo.logDaily rollover. During November 23rd, 2004, logging output will go to the file /wombat/foo.log. However, at midnight that file will archived as/wombat/foo.log.2004-11-23. For the 24th of November, logging output will be directed to/wombat/folder/foo.loguntil its archived as/wombat/foo.log.2004-11-24at the beginning of the next day.If configuring programatically, do not forget to call
activateOptions()method before using this policy. Moreover,activateOptions()ofTimeBasedRollingPolicymust be called before calling theactivateOptions()method of the owningRollingFileAppender.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringlastFileNameFile name at last rollover.private longnextCheckTime for next determination if time for rollover.private intsuffixLengthLength of any file type suffix (.gz, .zip).-
Fields inherited from class org.apache.log4j.rolling.RollingPolicyBase
activeFileName
-
-
Constructor Summary
Constructors Constructor Description TimeBasedRollingPolicy()Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivateOptions()Prepares instance of use.RolloverDescriptioninitialize(java.lang.String currentActiveFile, boolean append)Initialize the policy and return any initial actions for rolling file appender..booleanisTriggeringEvent(org.apache.log4j.Appender appender, org.apache.log4j.spi.LoggingEvent event, java.lang.String filename, long fileLength)Determines if a rollover may be appropriate at this time.RolloverDescriptionrollover(java.lang.String currentActiveFile)Prepare for a rollover.-
Methods inherited from class org.apache.log4j.rolling.RollingPolicyBase
formatFileName, getActiveFileName, getDatePatternConverter, getFileNamePattern, getIntegerPatternConverter, parseFileNamePattern, setActiveFileName, setFileNamePattern
-
-
-
-
Method Detail
-
activateOptions
public void activateOptions()
Prepares instance of use.- Specified by:
activateOptionsin interfaceorg.apache.log4j.spi.OptionHandler- Overrides:
activateOptionsin classRollingPolicyBase
-
initialize
public RolloverDescription initialize(java.lang.String currentActiveFile, boolean append)
Initialize the policy and return any initial actions for rolling file appender..- Specified by:
initializein interfaceRollingPolicy- Parameters:
currentActiveFile- 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 currentActiveFile)
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.- Specified by:
rolloverin interfaceRollingPolicy- Parameters:
currentActiveFile- file name for current active log file.- Returns:
- Description of pending rollover, may be null to indicate no rollover at this time.
-
isTriggeringEvent
public boolean isTriggeringEvent(org.apache.log4j.Appender appender, org.apache.log4j.spi.LoggingEvent event, java.lang.String filename, long fileLength)Determines if a rollover may be appropriate at this time. If true is returned, RolloverPolicy.rollover will be called but it can determine that a rollover is not warranted.- Specified by:
isTriggeringEventin interfaceTriggeringPolicy- Parameters:
appender- A reference to the appender.event- A reference to the currently event.filename- The filename for the currently active log file.fileLength- Length of the file in bytes.- Returns:
- true if a rollover should occur.
-
-