org.apache.log4j
Class DailyRollingFileAppenderExt

java.lang.Object
  |
  +--org.apache.log4j.AppenderSkeleton
        |
        +--org.apache.log4j.WriterAppender
              |
              +--org.apache.log4j.FileAppender
                    |
                    +--org.apache.log4j.DailyRollingFileAppenderExt
All Implemented Interfaces:
Appender, OptionHandler

public class DailyRollingFileAppenderExt
extends FileAppender

DailyRollingFileAppenderExt extends FileAppender so that the underlying file is rolled over at a user chosen frequency.

The rolling schedule is specified by the DatePattern option. This pattern should follow the SimpleDateFormat conventions. In particular, you must escape literal text within a pair of single quotes. A formatted version of the date pattern is used before the extension, if any, of the rolled file name.

For example, if the File option is set to /foo/bar.log and the DatePattern set to '.'yyyy-MM-dd, if today is 2001-02-16 then the logging file /foo/bar.2001-02-16.log will be used. At midnight logging will continue in the logging file /foo/bar.2001-02-17.log until it is rolled over itself the next day.

In the above example, if the File option had been set to /foo/bar instead, then the logging files would have been /foo/bar.2001-02-16 and /foo/bar.2001-02-17 respectively.

Is is possible to specify monthly, weekly, half-daily, daily, hourly, or minutely rollover schedules.

DatePattern Rollover schedule
'.'yyyy-MM Rollover at the beginning of each month
'.'yyyy-ww Rollover at the first day of each week. The first day of the week depends on the locale.
'.'yyyy-MM-dd Rollover at midnight each day.
'.'yyyy-MM-dd-a Rollover at midnight and midday of each day.
'.'yyyy-MM-dd-HH Rollover at the top of every hour.
'.'yyyy-MM-dd-HH-mm Rollover at the beginning of every minutue.

Do not use the colon ":" character in anywhere in the DatePattern option. The text before the colon is interpeted as the protocol specificaion of a URL which is probably not what you want.

Ripped largely from DailyRollingFileAppender but that wasn't written in an extensible way.

Author:
Don Taylor

Inner Class Summary
protected  class DailyRollingFileAppenderExt.LogFilter
          Used by deleteOldLogs() to determine the current set of log files that are in use.
protected static class DailyRollingFileAppenderExt.RollingCalendar
          RollingCalendar is a helper class to DailyRollingFileAppenderExt.
 
Field Summary
static String DATE_PATTERN_OPTION
          A string constant used in naming the option for setting the filename pattern.
static int HALF_DAY
           
protected static int MAX_LOGS
          Maximum number of logs to maintain.
static int TOP_OF_DAY
           
static int TOP_OF_HOUR
           
static int TOP_OF_MINUTE
           
static int TOP_OF_MONTH
           
static int TOP_OF_TROUBLE
           
static int TOP_OF_WEEK
           
 
Fields inherited from class org.apache.log4j.FileAppender
APPEND_OPTION, FILE_OPTION, fileAppend, fileName, qwIsOurs
 
Fields inherited from class org.apache.log4j.WriterAppender
IMMEDIATE_FLUSH_OPTION, immediateFlush, qw
 
Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold, THRESHOLD_OPTION
 
Constructor Summary
DailyRollingFileAppenderExt()
          The default constructor does nothing.
DailyRollingFileAppenderExt(Layout layout, String filename, String datePattern)
          Instantiate a DailyRollingFileAppenderExt and open the file designated by filename.
 
Method Summary
 void activateOptions()
           
protected  int computeCheckPeriod(String datePattern)
          Determines the type of check period for the specified datePattern.
protected  void deleteOldLogs()
          If the maxLogs property has been set, then this will delete the excess logs, oldest logs first.
 String getDatePattern()
          Returns the value of the DatePattern option.
 String getFile()
          Returns the base filename we're logging to.
 int getMaxLogs()
          The maximum number of logs being retained.
 boolean getWriteHeaderDuringAppend()
          Retrieve the writeHeaderDuringAppend property.
protected  void printPeriodicity(int type)
          Output to the debug log the rollover schedule.
protected  void rollOver()
          Rollover to a new file.
 void setDatePattern(String pattern)
          The DatePattern takes a string in the same format as expected by SimpleDateFormat.
 void setFile(String filename)
          Sets the base filename we're to log to.
 void setMaxLogs(int maxlogs)
          Set the maximum number of logs to retain.
 void setWriteHeaderDuringAppend(boolean flag)
          Sets the writeHeaderDuringAppend property.
protected  void subAppend(LoggingEvent event)
          This method differentiates DailyRollingFileAppenderExt from its super class.
protected  void writeHeader()
          Write the contents of this class' Layout.getHeader() to the log, unless we're appending to this log and the writeHeaderDuringAppend property is false.
 
Methods inherited from class org.apache.log4j.FileAppender
closeFile, getAppend, getOptionStrings, reset, setAppend, setFile, setOption, setQWForFiles
 
Methods inherited from class org.apache.log4j.WriterAppender
append, checkEntryConditions, close, closeWriter, getImmediateFlush, requiresLayout, setErrorHandler, setImmediateFlush, setWriter, writeFooter
 
Methods inherited from class org.apache.log4j.AppenderSkeleton
addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setLayout, setName, setThreshold
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOP_OF_TROUBLE

public static final int TOP_OF_TROUBLE

TOP_OF_MINUTE

public static final int TOP_OF_MINUTE

TOP_OF_HOUR

public static final int TOP_OF_HOUR

HALF_DAY

public static final int HALF_DAY

TOP_OF_DAY

public static final int TOP_OF_DAY

TOP_OF_WEEK

public static final int TOP_OF_WEEK

TOP_OF_MONTH

public static final int TOP_OF_MONTH

DATE_PATTERN_OPTION

public static final String DATE_PATTERN_OPTION
A string constant used in naming the option for setting the filename pattern. Current value of this string constant is DatePattern.

MAX_LOGS

protected static final int MAX_LOGS

Maximum number of logs to maintain.

If more than this number of logs exist, then the extra logs will be deleted, starting with the oldest logs. If this value is not greater than 0, then no logs will be deleted. Defaults to -1.

Constructor Detail

DailyRollingFileAppenderExt

public DailyRollingFileAppenderExt()
The default constructor does nothing.

DailyRollingFileAppenderExt

public DailyRollingFileAppenderExt(Layout layout,
                                   String filename,
                                   String datePattern)
                            throws IOException
Instantiate a DailyRollingFileAppenderExt and open the file designated by filename. The opened filename will become the ouput destination for this appender.
Method Detail

setDatePattern

public void setDatePattern(String pattern)
The DatePattern takes a string in the same format as expected by SimpleDateFormat. This options determines the rollover schedule.

getDatePattern

public String getDatePattern()
Returns the value of the DatePattern option.

activateOptions

public void activateOptions()
Overrides:
activateOptions in class FileAppender

printPeriodicity

protected void printPeriodicity(int type)

Output to the debug log the rollover schedule.

See Also:
LogLog

computeCheckPeriod

protected int computeCheckPeriod(String datePattern)

Determines the type of check period for the specified datePattern.

Returns:
One of
  • TOP_OF_MINUTE
  • TOP_OF_HOUR
  • HALF_DAY
  • TOP_OF_DAY
  • TOP_OF_WEEK
  • TOP_OF_MONTH

rollOver

protected void rollOver()
                 throws IOException
Rollover to a new file.

subAppend

protected void subAppend(LoggingEvent event)
This method differentiates DailyRollingFileAppenderExt from its super class.
Overrides:
subAppend in class WriterAppender

setFile

public void setFile(String filename)

Sets the base filename we're to log to. This is not the actual file that will be logged to, as the actual file will include a date/time stamp.

Overrides:
setFile in class FileAppender

getFile

public String getFile()

Returns the base filename we're logging to.

Overrides:
getFile in class FileAppender
See Also:
setFile(java.lang.String)

setMaxLogs

public void setMaxLogs(int maxlogs)

Set the maximum number of logs to retain. Excess logs will be deleted, oldest logs first. If the value <= 0; then no logs will be deleted.

See Also:
deleteOldLogs()

getMaxLogs

public int getMaxLogs()

The maximum number of logs being retained.

See Also:
setMaxLogs(int), deleteOldLogs()

deleteOldLogs

protected void deleteOldLogs()

If the maxLogs property has been set, then this will delete the excess logs, oldest logs first.

See Also:
setMaxLogs(int)

setWriteHeaderDuringAppend

public void setWriteHeaderDuringAppend(boolean flag)

Sets the writeHeaderDuringAppend property. If true, then when appending to an already existing log, the contents of the associated Layout.getHeader() will be written to the log, otherwise it will not.

This property exists to solve a problem when using HTMLLayout-based layouts, it is an error to write an HTML header multiple times to an HTML file.


getWriteHeaderDuringAppend

public boolean getWriteHeaderDuringAppend()

Retrieve the writeHeaderDuringAppend property.

See Also:
setWriteHeaderDuringAppend(boolean)

writeHeader

protected void writeHeader()

Write the contents of this class' Layout.getHeader() to the log, unless we're appending to this log and the writeHeaderDuringAppend property is false.

Overrides:
writeHeader in class WriterAppender
See Also:
setWriteHeaderDuringAppend(boolean)