FileAppenderpublic class FileAppender extends WriterAppender FileAppender appends log events to a file.
Support for java.io.Writer and console appending
has been deprecated and then removed. See the replacement
solutions: {@link WriterAppender} and {@link ConsoleAppender}. |
Fields Summary |
---|
protected boolean | fileAppendControls file truncatation. The default value for this variable
is true , meaning that by default a
FileAppender will append to an existing file and not
truncate it.
This option is meaningful only if the FileAppender opens the
file. | protected String | fileNameThe name of the log file. | protected boolean | bufferedIODo we do bufferedIO? | protected int | bufferSizeDetermines the size of IO buffer be. Default is 8K. |
Constructors Summary |
---|
public FileAppender()The default constructor does not do anything.
| public FileAppender(Layout layout, String filename, boolean append, boolean bufferedIO, int bufferSize)Instantiate a FileAppender and open the file
designated by filename . The opened filename will
become the output destination for this appender.
If the append parameter is true, the file will be
appended to. Otherwise, the file designated by
filename will be truncated before being opened.
If the bufferedIO parameter is true ,
then buffered IO will be used to write to the output file.
this.layout = layout;
this.setFile(filename, append, bufferedIO, bufferSize);
| public FileAppender(Layout layout, String filename, boolean append)Instantiate a FileAppender and open the file designated by
filename . The opened filename will become the output
destination for this appender.
If the append parameter is true, the file will be
appended to. Otherwise, the file designated by
filename will be truncated before being opened.
this.layout = layout;
this.setFile(filename, append, false, bufferSize);
| public FileAppender(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.
this(layout, filename, true);
|
Methods Summary |
---|
public void | activateOptions()If the value of File is not null , then {@link
#setFile} is called with the values of File and
Append properties.
if(fileName != null) {
try {
setFile(fileName, fileAppend, bufferedIO, bufferSize);
}
catch(java.io.IOException e) {
errorHandler.error("setFile("+fileName+","+fileAppend+") call failed.",
e, ErrorCode.FILE_OPEN_FAILURE);
}
} else {
//LogLog.error("File option not set for appender ["+name+"].");
LogLog.warn("File option not set for appender ["+name+"].");
LogLog.warn("Are you using FileAppender instead of ConsoleAppender?");
}
| protected void | closeFile()Closes the previously opened file.
if(this.qw != null) {
try {
this.qw.close();
}
catch(java.io.IOException e) {
// Exceptionally, it does not make sense to delegate to an
// ErrorHandler. Since a closed appender is basically dead.
LogLog.error("Could not close " + qw, e);
}
}
| public boolean | getAppend()Returns the value of the Append option.
return fileAppend;
| public int | getBufferSize()Get the size of the IO buffer.
return this.bufferSize;
| public boolean | getBufferedIO()Get the value of the BufferedIO option.
BufferedIO will significatnly increase performance on heavily
loaded systems.
return this.bufferedIO;
| public java.lang.String | getFile()Returns the value of the File option.
return fileName;
| protected void | reset()Close any previously opened file and call the parent's
reset .
closeFile();
this.fileName = null;
super.reset();
| public void | setAppend(boolean flag)The Append option takes a boolean value. It is set to
true by default. If true, then File
will be opened in append mode by {@link #setFile setFile} (see
above). Otherwise, {@link #setFile setFile} will open
File in truncate mode.
Note: Actual opening of the file is made when {@link
#activateOptions} is called, not when the options are set.
fileAppend = flag;
| public void | setBufferSize(int bufferSize)Set the size of the IO buffer.
this.bufferSize = bufferSize;
| public void | setBufferedIO(boolean bufferedIO)The BufferedIO option takes a boolean value. It is set to
false by default. If true, then File
will be opened and the resulting {@link java.io.Writer} wrapped
around a {@link BufferedWriter}.
BufferedIO will significatnly increase performance on heavily
loaded systems.
this.bufferedIO = bufferedIO;
if(bufferedIO) {
immediateFlush = false;
}
| public synchronized void | setFile(java.lang.String fileName, boolean append, boolean bufferedIO, int bufferSize)Sets and opens the file where the log output will
go. The specified file must be writable.
If there was already an opened file, then the previous file
is closed first.
Do not use this method directly. To configure a FileAppender
or one of its subclasses, set its properties one by one and then
call activateOptions.
LogLog.debug("setFile called: "+fileName+", "+append);
// It does not make sense to have immediate flush and bufferedIO.
if(bufferedIO) {
setImmediateFlush(false);
}
reset();
FileOutputStream ostream = null;
try {
//
// attempt to create file
//
ostream = new FileOutputStream(fileName, append);
} catch(FileNotFoundException ex) {
//
// if parent directory does not exist then
// attempt to create it and try to create file
// see bug 9150
//
String parentName = new File(fileName).getParent();
if (parentName != null) {
File parentDir = new File(parentName);
if(!parentDir.exists() && parentDir.mkdirs()) {
ostream = new FileOutputStream(fileName, append);
} else {
throw ex;
}
} else {
throw ex;
}
}
Writer fw = createWriter(ostream);
if(bufferedIO) {
fw = new BufferedWriter(fw, bufferSize);
}
this.setQWForFiles(fw);
this.fileName = fileName;
this.fileAppend = append;
this.bufferedIO = bufferedIO;
this.bufferSize = bufferSize;
writeHeader();
LogLog.debug("setFile ended");
| public void | setFile(java.lang.String file)The File property takes a string value which should be the
name of the file to append to.
Note that the special values
"System.out" or "System.err" are no longer honored.
Note: Actual opening of the file is made when {@link
#activateOptions} is called, not when the options are set.
// Trim spaces from both ends. The users probably does not want
// trailing spaces in file names.
String val = file.trim();
fileName = val;
| protected void | setQWForFiles(java.io.Writer writer)Sets the quiet writer being used.
This method is overriden by {@link RollingFileAppender}.
this.qw = new QuietWriter(writer, errorHandler);
|
|