DailyFileAppenderpublic class DailyFileAppender extends FileAppender DailyFileAppender extends FileAppender to use filenames formatted with
date/time information. The filename is recomputed every day at midnight.
Note that the filename doesn't have to change every day, making it possible
to have logfiles which are per-week or per-month.
The appender computes the proper filename using the formats specified in
java.text.SimpleDateFormat. The format requires that most static text is
enclosed in single quotes, which are removed. The examples below show how
quotes are used to embed static information in the format.
Sample filenames:
Filename pattern Filename
"'/logs/trace-'yyyy-MM-dd'.log'" /logs/trace-2000-12-31.log
"'/logs/trace-'yyyy-ww'.log'" /logs/trace-2000-52.log
|
Fields Summary |
---|
public static final String | FILE_NAME_PATTERN_OPTIONA string constant used in naming the option for setting the
filename pattern. Current value of this string constant is
FileNamePattern. | private String | fileNamePatternThe filename pattern | private String | currentFileNameThe actual formatted filename that is currently being written to | private long | nextFilenameComputingMillisThe timestamp when we shall next recompute the filename |
Constructors Summary |
---|
public DailyFileAppender()The default constructor does no longer set a default layout nor a
default output target.
| public DailyFileAppender(Layout layout, String filename, boolean append)Instantiate a RollingFileAppender 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.
super(layout, filename, append);
| public DailyFileAppender(Layout layout, String filename)Instantiate a FileAppender and open the file designated by
filename . The opened filename will become the output
destination for this appender.
The file will be appended to.
super(layout, filename);
|
Methods Summary |
---|
public void | activateOptions()If the a value for {@link #FILE_OPTION} is non-null, then {@link
#setFile} is called with the values of {@link #FILE_OPTION} and
{@link #APPEND_OPTION}.
try {
setFile(null, super.fileAppend);
}
catch(java.io.IOException e) {
errorHandler.error("setFile(null,"+fileAppend+") call failed.",
e, ErrorCode.FILE_OPEN_FAILURE);
}
| public java.lang.String[] | getOptionStrings()Retuns the option names for this component, namely {@link
#FILE_NAME_PATTERN_OPTION} in
addition to the options of {@link FileAppender#getOptionStrings
FileAppender}.
return OptionConverter.concatanateArrays(super.getOptionStrings(),
new String[] {FILE_NAME_PATTERN_OPTION});
| public synchronized void | setFile(java.lang.String fileName, boolean append)Set the current output file.
The function will compute a new filename, and open a new file only
when the name has changed.
The function is automatically called once a day, to allow for
daily files -- the purpose of this class.
/* Compute filename, but only if fileNamePattern is specified */
if (fileNamePattern == null) {
errorHandler.error("Missing file pattern (" + FILE_NAME_PATTERN_OPTION + ") in setFile().");
return;
}
Date now = new Date();
fileName = new SimpleDateFormat(fileNamePattern).format (now);
if (fileName.equals(currentFileName))
return;
/* Set up next filename checkpoint */
DailyFileAppenderCalendar c = new DailyFileAppenderCalendar();
c.rollToNextDay ();
nextFilenameComputingMillis = c.getTimeInMillis ();
currentFileName = fileName;
super.setFile(fileName, append);
| public void | setOption(java.lang.String key, java.lang.String value)Set the options for the appender
super.setOption(key, value);
if(key.equalsIgnoreCase(FILE_NAME_PATTERN_OPTION)) {
fileNamePattern = value;
}
| protected void | subAppend(org.apache.log4j.spi.LoggingEvent event)This method differentiates RollingFileAppender from its super
class.
if (System.currentTimeMillis () >= nextFilenameComputingMillis) {
try {
setFile (super.fileName, super.fileAppend);
}
catch(IOException e) {
System.err.println("setFile(null, false) call failed.");
e.printStackTrace();
}
}
super.subAppend(event);
|
|