Save This Page
Home » apache-log4j-1.2.15 » org.apache » log4j » [javadoc | source]
org.apache.log4j
public class: CompositeRollingAppender [javadoc | source]
java.lang.Object
   org.apache.log4j.AppenderSkeleton
      org.apache.log4j.WriterAppender
         org.apache.log4j.FileAppender
            org.apache.log4j.CompositeRollingAppender

All Implemented Interfaces:
    OptionHandler, Appender

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
static final  int TOP_OF_TROUBLE     
static final  int TOP_OF_MINUTE     
static final  int TOP_OF_HOUR     
static final  int HALF_DAY     
static final  int TOP_OF_DAY     
static final  int TOP_OF_WEEK     
static final  int TOP_OF_MONTH     
static final  int BY_SIZE    Style of rolling to use 
static final  int BY_DATE     
static final  int BY_COMPOSITE     
static final  String S_BY_SIZE     
static final  String S_BY_DATE     
static final  String S_BY_COMPOSITE     
 Date now    Holds date of last roll over 
 SimpleDateFormat sdf     
 RollingCalendar rc    Helper class to determine next rollover time 
 int checkPeriod    Current period for roll overs 
protected  long maxFileSize    The default maximum file size is 10MB. 
protected  int maxSizeRollBackups    There is zero backup files by default. 
protected  int curSizeRollBackups    How many sized based backups have been made so far 
protected  int maxTimeRollBackups    not yet implemented 
protected  int curTimeRollBackups     
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. 
protected  int rollingStyle    Style of rolling to Use. BY_SIZE (1), BY_DATE(2), BY COMPOSITE(3) 
protected  boolean rollDate     
protected  boolean rollSize     
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! 
protected  String baseFileName    FileName provided in configuration. Used for rolling properly 
Fields inherited from org.apache.log4j.FileAppender:
fileAppend,  fileName,  bufferedIO,  bufferSize
Fields inherited from org.apache.log4j.WriterAppender:
immediateFlush,  encoding,  qw
Fields inherited from org.apache.log4j.AppenderSkeleton:
layout,  name,  threshold,  errorHandler,  headFilter,  tailFilter,  closed
Constructor:
 public CompositeRollingAppender() 
 public CompositeRollingAppender(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.

 public CompositeRollingAppender(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.
 public CompositeRollingAppender(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.

 public CompositeRollingAppender(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.
Method from org.apache.log4j.CompositeRollingAppender Summary:
activateOptions,   computeCheckPeriod,   deleteFile,   existingInit,   getCountDirection,   getDatePattern,   getMaxSizeRollBackups,   getMaximumFileSize,   getRollingStyle,   getStaticLogFileName,   rollFile,   rollOverSize,   rollOverTime,   setCountDirection,   setDatePattern,   setFile,   setFile,   setMaxFileSize,   setMaxFileSize,   setMaxSizeRollBackups,   setMaximumFileSize,   setQWForFiles,   setRollingStyle,   setStaticLogFileName,   setStaticLogFileName,   subAppend
Methods from org.apache.log4j.FileAppender:
activateOptions,   closeFile,   getAppend,   getBufferSize,   getBufferedIO,   getFile,   reset,   setAppend,   setBufferSize,   setBufferedIO,   setFile,   setFile,   setQWForFiles
Methods from org.apache.log4j.WriterAppender:
activateOptions,   append,   checkEntryConditions,   close,   closeWriter,   createWriter,   getEncoding,   getImmediateFlush,   requiresLayout,   reset,   setEncoding,   setErrorHandler,   setImmediateFlush,   setWriter,   subAppend,   writeFooter,   writeHeader
Methods from org.apache.log4j.AppenderSkeleton:
activateOptions,   addFilter,   append,   clearFilters,   doAppend,   finalize,   getErrorHandler,   getFilter,   getFirstFilter,   getLayout,   getName,   getThreshold,   isAsSevereAsThreshold,   setErrorHandler,   setLayout,   setName,   setThreshold
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.log4j.CompositeRollingAppender Detail:
 public  void activateOptions() 
    Sets initial conditions including date/time roll over information, first check, scheduledFilename, and calls existingInit to initialize the current # of backups.
 int computeCheckPeriod() 
 protected static  void deleteFile(String fileName) 
    Delete's the specified file if it exists
 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.
 public int getCountDirection() 
 public String getDatePattern() 
    Returns the value of the DatePattern option.
 public int getMaxSizeRollBackups() 
    Returns the value of the maxSizeRollBackups option.
 public long getMaximumFileSize() 
    Get the maximum size that the output file is allowed to reach before being rolled over to backup files.
 public int getRollingStyle() 
 public boolean getStaticLogFileName() 
 protected static  void rollFile(String from,
    String to) 
    Renames file from to file to. It also checks for existence of target file and deletes if it does.
 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.

 protected  void rollOverTime() 
    Rollover the file(s) to date/time tagged file(s). Opens the new file (through setFile) and resets curSizeRollBackups.
 public  void setCountDirection(int direction) 
 public  void setDatePattern(String pattern) 
    The DatePattern takes a string in the same format as expected by SimpleDateFormat . This options determines the rollover schedule.
 public  void setFile(String file) 
 public synchronized  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.
 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 except that it is required for differentiating the setter taking a long argument from the setter taking a String argument by the JavaBeans Introspector .

 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.

 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 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)

 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 except that it is required for differentiating the setter taking a long argument from the setter taking a String argument by the JavaBeans Introspector .

 protected  void setQWForFiles(Writer writer) 
 public  void setRollingStyle(int style) 
 public  void setStaticLogFileName(boolean s) 
 public  void setStaticLogFileName(String value) 
 protected  void subAppend(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.