org.apache.log4j
Class QpidCompositeRollingAppender

java.lang.Object
  extended by org.apache.log4j.AppenderSkeleton
      extended by org.apache.log4j.WriterAppender
          extended by org.apache.log4j.FileAppender
              extended by org.apache.log4j.QpidCompositeRollingAppender
All Implemented Interfaces:
org.apache.log4j.Appender, org.apache.log4j.spi.OptionHandler

public class QpidCompositeRollingAppender
extends org.apache.log4j.FileAppender

CompositeRollingAppender combines RollingFileAppender and DailyRollingFileAppender
It can function as either or do both at the same time (making size based rolling files like RollingFileAppender until a data/time boundary is crossed at which time it rolls all of those files as per the DailyRollingFileAppender) based on the setting for rollingStyle.

To use CompositeRollingAppender to roll log files as they reach a certain size (like RollingFileAppender), set rollingStyle=1 (@see config.size)
To use CompositeRollingAppender to roll log files at certain time intervals (daily for example), set rollingStyle=2 and a datePattern (@see config.time)
To have CompositeRollingAppender roll log files at a certain size AND rename those according to time intervals, set rollingStyle=3 (@see config.composite)

A of few additional optional features have been added:
-- Attach date pattern for current log file (@see staticLogFileName)
-- Backup number increments for newer files (@see countDirection)
-- Infinite number of backups by file size (@see maxSizeRollBackups)

A few notes and warnings: For large or infinite number of backups countDirection > 0 is highly recommended, with staticLogFileName = false if time based rolling is also used -- this will reduce the number of file renamings to few or none. Changing staticLogFileName or countDirection without clearing the directory could have nasty side effects. If Date/Time based rolling is enabled, CompositeRollingAppender will attempt to roll existing files in the directory without a date/time tag based on the last modified date of the base log files last modification.

A maximum number of backups based on date/time boundries would be nice but is not yet implemented.


Field Summary
protected  String backupFilesToPath
          Path provided in configuration.
protected  String baseFileName
          FileName provided in configuration.
(package private) static int BY_COMPOSITE
           
(package private) static int BY_DATE
           
(package private) static int BY_SIZE
          Style of rolling to use
protected  boolean compress
          Do we want to .gz our backup files.
protected  boolean compressAsync
          Do we want to use a second thread when compressing our backup files.
(package private)  org.apache.log4j.QpidCompositeRollingAppender.Compressor compressor
           
protected  int countDirection
          By default newer files have lower numbers.
protected  int curSizeRollBackups
          How many sized based backups have been made so far
protected  int curTimeRollBackups
           
(package private)  Executor executor
           
(package private) static int HALF_DAY
           
protected  long maxFileSize
          The default maximum file size is 10MB.
protected  int maxSizeRollBackups
          There is zero backup files by default.
protected  int maxTimeRollBackups
          not yet implemented
(package private)  Date now
          Holds date of last roll over
(package private)  org.apache.log4j.RollingCalendar rc
          Helper class to determine next rollover time
protected  boolean rollDate
           
protected  int rollingStyle
          Style of rolling to Use.
protected  boolean rollSize
           
(package private) static String S_BY_COMPOSITE
           
(package private) static String S_BY_DATE
           
(package private) static String S_BY_SIZE
           
(package private)  SimpleDateFormat sdf
           
protected  boolean staticLogFileName
          By default file.log is always the current file.
(package private) static int TOP_OF_DAY
           
(package private) static int TOP_OF_HOUR
           
(package private) static int TOP_OF_MINUTE
           
(package private) static int TOP_OF_MONTH
           
(package private) static int TOP_OF_TROUBLE
           
(package private) static int TOP_OF_WEEK
           
protected  boolean zeroBased
          Do we want to start numbering files at zero.
 
Fields inherited from class org.apache.log4j.FileAppender
bufferedIO, bufferSize, fileAppend, fileName
 
Fields inherited from class org.apache.log4j.WriterAppender
encoding, immediateFlush, qw
 
Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold
 
Constructor Summary
QpidCompositeRollingAppender()
          The default constructor does nothing.
QpidCompositeRollingAppender(org.apache.log4j.Layout layout, String filename)
          Instantiate a CompositeRollingAppender and open the file designated by filename.
QpidCompositeRollingAppender(org.apache.log4j.Layout layout, String filename, boolean append)
          Instantiate a CompositeRollingAppender and open the file designated by filename.
QpidCompositeRollingAppender(org.apache.log4j.Layout layout, String filename, String datePattern)
          Instantiate a CompositeRollingAppender and open the file designated by filename.
QpidCompositeRollingAppender(org.apache.log4j.Layout layout, String filename, String datePattern, boolean append)
          Instantiate a CompositeRollingAppender and open the file designated by filename.
 
Method Summary
 void activateOptions()
          Sets initial conditions including date/time roll over information, first check, scheduledFilename, and calls existingInit to initialize the current # of backups.
(package private)  int computeCheckPeriod()
           
protected  void deleteFile(String relativeFileName)
          Delete the given file that is prepended with the relative path to the log directory.
protected  void doCompress(File from, File to)
           
protected  void existingInit()
          Initializes based on exisiting conditions at time of activateOptions.
 String getBackupFilesToPath()
           
 boolean getCompressAsync()
           
 boolean getCompressBackupFiles()
           
 int getCountDirection()
           
 String getDatePattern()
          Returns the value of the DatePattern option.
 long getMaximumFileSize()
          Get the maximum size that the output file is allowed to reach before being rolled over to backup files.
 int getMaxSizeRollBackups()
          Returns the value of the maxSizeRollBackups option.
 int getRollingStyle()
           
 boolean getStaticLogFileName()
           
 boolean getZeroBased()
           
protected  void rollFile(String from, String to, boolean compress)
          Renames file from to file to.
protected  void rollOverSize()
          Implements roll overs base on file size.
protected  void rollOverTime()
          Rollover the file(s) to date/time tagged file(s).
 void setbackupFilesToPath(String path)
           
 void setCompressAsync(boolean c)
           
 void setCompressBackupFiles(boolean c)
           
 void setCountDirection(int direction)
           
 void setDatePattern(String pattern)
          The DatePattern takes a string in the same format as expected by SimpleDateFormat.
 void setFile(String file)
           
 void setFile(String fileName, boolean append)
          Creates and opens the file for logging.
 void setMaxFileSize(long maxFileSize)
          Set the maximum size that the output file is allowed to reach before being rolled over to backup files.
 void setMaxFileSize(String value)
          Set the maximum size that the output file is allowed to reach before being rolled over to backup files.
 void setMaximumFileSize(long maxFileSize)
          Set the maximum size that the output file is allowed to reach before being rolled over to backup files.
 void setMaxSizeRollBackups(int maxBackups)
          Set the maximum number of backup files to keep around based on file size.
protected  void setQWForFiles(Writer writer)
           
 void setRollingStyle(int style)
           
 void setStaticLogFileName(boolean s)
           
 void setStaticLogFileName(String value)
           
 void setZeroBased(boolean z)
           
protected  void subAppend(org.apache.log4j.spi.LoggingEvent event)
          Handles append time behavior for CompositeRollingAppender.
 
Methods inherited from class org.apache.log4j.FileAppender
closeFile, getAppend, getBufferedIO, getBufferSize, getFile, reset, setAppend, setBufferedIO, setBufferSize, setFile
 
Methods inherited from class org.apache.log4j.WriterAppender
append, checkEntryConditions, close, closeWriter, createWriter, getEncoding, getImmediateFlush, requiresLayout, setEncoding, setErrorHandler, setImmediateFlush, setWriter, shouldFlush, writeFooter, writeHeader
 
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

static final int TOP_OF_TROUBLE
See Also:
Constant Field Values

TOP_OF_MINUTE

static final int TOP_OF_MINUTE
See Also:
Constant Field Values

TOP_OF_HOUR

static final int TOP_OF_HOUR
See Also:
Constant Field Values

HALF_DAY

static final int HALF_DAY
See Also:
Constant Field Values

TOP_OF_DAY

static final int TOP_OF_DAY
See Also:
Constant Field Values

TOP_OF_WEEK

static final int TOP_OF_WEEK
See Also:
Constant Field Values

TOP_OF_MONTH

static final int TOP_OF_MONTH
See Also:
Constant Field Values

BY_SIZE

static final int BY_SIZE
Style of rolling to use

See Also:
Constant Field Values

BY_DATE

static final int BY_DATE
See Also:
Constant Field Values

BY_COMPOSITE

static final int BY_COMPOSITE
See Also:
Constant Field Values

S_BY_SIZE

static final String S_BY_SIZE
See Also:
Constant Field Values

S_BY_DATE

static final String S_BY_DATE
See Also:
Constant Field Values

S_BY_COMPOSITE

static final String S_BY_COMPOSITE
See Also:
Constant Field Values

now

Date now
Holds date of last roll over


sdf

SimpleDateFormat sdf

rc

org.apache.log4j.RollingCalendar rc
Helper class to determine next rollover time


maxFileSize

protected long maxFileSize
The default maximum file size is 10MB.


maxSizeRollBackups

protected int maxSizeRollBackups
There is zero backup files by default.


curSizeRollBackups

protected int curSizeRollBackups
How many sized based backups have been made so far


maxTimeRollBackups

protected int maxTimeRollBackups
not yet implemented


curTimeRollBackups

protected int curTimeRollBackups

countDirection

protected int countDirection
By default newer files have lower numbers. (countDirection < 0) ie. log.1 is most recent, log.5 is the 5th backup, etc... countDirection > 0 does the opposite ie. log.1 is the first backup made, log.5 is the 5th backup made, etc. For infinite backups use countDirection > 0 to reduce rollOver costs.


rollingStyle

protected int rollingStyle
Style of rolling to Use. BY_SIZE (1), BY_DATE(2), BY COMPOSITE(3)


rollDate

protected boolean rollDate

rollSize

protected boolean rollSize

staticLogFileName

protected boolean staticLogFileName
By default file.log is always the current file. Optionally file.log.yyyy-mm-dd for current formated datePattern can by the currently logging file (or file.log.curSizeRollBackup or even file.log.yyyy-mm-dd.curSizeRollBackup) This will make time based roll overs with a large number of backups much faster -- it won't have to rename all the backups!


baseFileName

protected String baseFileName
FileName provided in configuration. Used for rolling properly


compress

protected boolean compress
Do we want to .gz our backup files.


compressAsync

protected boolean compressAsync
Do we want to use a second thread when compressing our backup files.


zeroBased

protected boolean zeroBased
Do we want to start numbering files at zero.


backupFilesToPath

protected String backupFilesToPath
Path provided in configuration. Used for moving backup files to


compressor

org.apache.log4j.QpidCompositeRollingAppender.Compressor compressor

executor

Executor executor
Constructor Detail

QpidCompositeRollingAppender

public QpidCompositeRollingAppender()
The default constructor does nothing.


QpidCompositeRollingAppender

public QpidCompositeRollingAppender(org.apache.log4j.Layout layout,
                                    String filename,
                                    String datePattern)
                             throws IOException
Instantiate a CompositeRollingAppender and open the file designated by filename. The opened filename will become the ouput destination for this appender.

Throws:
IOException

QpidCompositeRollingAppender

public QpidCompositeRollingAppender(org.apache.log4j.Layout layout,
                                    String filename,
                                    boolean append)
                             throws IOException
Instantiate a CompositeRollingAppender and open the file designated by filename. The opened filename will become the ouput destination for this appender.

If the append parameter is true, the file will be appended to. Otherwise, the file desginated by filename will be truncated before being opened.

Throws:
IOException

QpidCompositeRollingAppender

public QpidCompositeRollingAppender(org.apache.log4j.Layout layout,
                                    String filename,
                                    String datePattern,
                                    boolean append)
                             throws IOException
Instantiate a CompositeRollingAppender and open the file designated by filename. The opened filename will become the ouput destination for this appender.

Throws:
IOException

QpidCompositeRollingAppender

public QpidCompositeRollingAppender(org.apache.log4j.Layout layout,
                                    String filename)
                             throws IOException
Instantiate a CompositeRollingAppender and open the file designated by filename. The opened filename will become the output destination for this appender.

The file will be appended to. DatePattern is default.

Throws:
IOException
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.


getMaxSizeRollBackups

public int getMaxSizeRollBackups()
Returns the value of the maxSizeRollBackups option.


getMaximumFileSize

public long getMaximumFileSize()
Get the maximum size that the output file is allowed to reach before being rolled over to backup files.

Since:
1.1

setMaxSizeRollBackups

public void setMaxSizeRollBackups(int maxBackups)

Set the maximum number of backup files to keep around based on file size.

The MaxSizeRollBackups option determines how many backup files are kept before the oldest is erased. This option takes an integer value. If set to zero, then there will be no backup files and the log file will be truncated when it reaches MaxFileSize. If a negative number is supplied then no deletions will be made. Note that this could result in very slow performance as a large number of files are rolled over unless setCountDirection(int) up is used.

The maximum applys to -each- time based group of files and -not- the total. Using a daily roll the maximum total files would be (#days run) * (maxSizeRollBackups)


setMaxFileSize

public void setMaxFileSize(long maxFileSize)
Set the maximum size that the output file is allowed to reach before being rolled over to backup files.

This method is equivalent to setMaxFileSize(long) except that it is required for differentiating the setter taking a long argument from the setter taking a String argument by the JavaBeans Introspector.

See Also:
setMaxFileSize(String)

setMaximumFileSize

public void setMaximumFileSize(long maxFileSize)
Set the maximum size that the output file is allowed to reach before being rolled over to backup files.

This method is equivalent to setMaxFileSize(long) except that it is required for differentiating the setter taking a long argument from the setter taking a String argument by the JavaBeans Introspector.

See Also:
setMaxFileSize(String)

setMaxFileSize

public void setMaxFileSize(String value)
Set the maximum size that the output file is allowed to reach before being rolled over to backup files.

In configuration files, the MaxFileSize option takes an long integer in the range 0 - 2^63. You can specify the value with the suffixes "KB", "MB" or "GB" so that the integer is interpreted being expressed respectively in kilobytes, megabytes or gigabytes. For example, the value "10KB" will be interpreted as 10240.


setQWForFiles

protected void setQWForFiles(Writer writer)
Overrides:
setQWForFiles in class org.apache.log4j.FileAppender

computeCheckPeriod

int computeCheckPeriod()

subAppend

protected void subAppend(org.apache.log4j.spi.LoggingEvent event)
Handles append time behavior for CompositeRollingAppender. This checks if a roll over either by date (checked first) or time (checked second) is need and then appends to the file last.

Overrides:
subAppend in class org.apache.log4j.WriterAppender

setFile

public void setFile(String file)
Overrides:
setFile in class org.apache.log4j.FileAppender

setFile

public void setFile(String fileName,
                    boolean append)
             throws IOException
Creates and opens the file for logging. If staticLogFileName is false then the fully qualified name is determined and used.

Throws:
IOException

getCountDirection

public int getCountDirection()

setCountDirection

public void setCountDirection(int direction)

getRollingStyle

public int getRollingStyle()

setRollingStyle

public void setRollingStyle(int style)

getStaticLogFileName

public boolean getStaticLogFileName()

setStaticLogFileName

public void setStaticLogFileName(boolean s)

setStaticLogFileName

public void setStaticLogFileName(String value)

getCompressBackupFiles

public boolean getCompressBackupFiles()

setCompressBackupFiles

public void setCompressBackupFiles(boolean c)

getCompressAsync

public boolean getCompressAsync()

setCompressAsync

public void setCompressAsync(boolean c)

getZeroBased

public boolean getZeroBased()

setZeroBased

public void setZeroBased(boolean z)

getBackupFilesToPath

public String getBackupFilesToPath()

setbackupFilesToPath

public void setbackupFilesToPath(String path)

existingInit

protected void existingInit()
Initializes based on exisiting conditions at time of activateOptions. The following is done:

A) determine curSizeRollBackups
B) determine curTimeRollBackups (not implemented)
C) initiates a roll over if needed for crossing a date boundary since the last run.


activateOptions

public void activateOptions()
Sets initial conditions including date/time roll over information, first check, scheduledFilename, and calls existingInit to initialize the current # of backups.

Specified by:
activateOptions in interface org.apache.log4j.spi.OptionHandler
Overrides:
activateOptions in class org.apache.log4j.FileAppender

rollOverTime

protected void rollOverTime()
Rollover the file(s) to date/time tagged file(s). Opens the new file (through setFile) and resets curSizeRollBackups.


rollFile

protected void rollFile(String from,
                        String to,
                        boolean compress)
Renames file from to file to. It also checks for existence of target file and deletes if it does.


deleteFile

protected void deleteFile(String relativeFileName)
Delete the given file that is prepended with the relative path to the log directory. Compress is enabled check for file with COMPRESS_EXTENSION(.gz) if backupFilesToPath is set then check in this directory not the main log directory.


rollOverSize

protected void rollOverSize()
Implements roll overs base on file size.

If the maximum number of size based backups is reached (curSizeRollBackups == maxSizeRollBackups If countDirection < 0, then files {File.1, ..., File.curSizeRollBackups -1} are renamed to {File.2, ..., File.curSizeRollBackups}. Moreover, File is renamed File.1 and closed.
A new file is created to receive further log output.

If maxSizeRollBackups is equal to zero, then the File is truncated with no backup files created.

If maxSizeRollBackups < 0, then File is renamed if needed and no files are deleted.


doCompress

protected void doCompress(File from,
                          File to)


Licensed to the Apache Software Foundation