Methods Summary |
---|
public void | activateOptions()Does nothing.
|
public void | append(org.apache.log4j.spi.LoggingEvent event)This method is called by the {@link AppenderSkeleton#doAppend}
method.
If the output stream exists and is writable then write a log
statement to the output stream. Otherwise, write a single warning
message to System.err .
The format of the output will depend on this appender's
layout.
// Reminder: the nesting of calls is:
//
// doAppend()
// - check threshold
// - filter
// - append();
// - checkEntryConditions();
// - subAppend();
if(!checkEntryConditions()) {
return;
}
subAppend(event);
|
protected boolean | checkEntryConditions()This method determines if there is a sense in attempting to append.
It checks whether there is a set output target and also if
there is a set layout. If these checks fail, then the boolean
value false is returned.
if(this.closed) {
LogLog.warn("Not allowed to write to a closed appender.");
return false;
}
if(this.qw == null) {
errorHandler.error("No output stream or file set for the appender named ["+
name+"].");
return false;
}
if(this.layout == null) {
errorHandler.error("No layout set for the appender named ["+ name+"].");
return false;
}
return true;
|
public synchronized void | close()Close this appender instance. The underlying stream or writer is
also closed.
Closed appenders cannot be reused.
if(this.closed)
return;
this.closed = true;
writeFooter();
reset();
|
protected void | closeWriter()Close the underlying {@link java.io.Writer}.
if(qw != null) {
try {
qw.close();
} catch(IOException e) {
// There is do need to invoke an error handler at this late
// stage.
LogLog.error("Could not close " + qw, e);
}
}
|
protected java.io.OutputStreamWriter | createWriter(java.io.OutputStream os)Returns an OutputStreamWriter when passed an OutputStream. The
encoding used will depend on the value of the
encoding property. If the encoding value is
specified incorrectly the writer will be opened using the default
system encoding (an error message will be printed to the loglog.
OutputStreamWriter retval = null;
String enc = getEncoding();
if(enc != null) {
try {
retval = new OutputStreamWriter(os, enc);
} catch(IOException e) {
LogLog.warn("Error initializing output writer.");
LogLog.warn("Unsupported encoding?");
}
}
if(retval == null) {
retval = new OutputStreamWriter(os);
}
return retval;
|
public java.lang.String | getEncoding()
return encoding;
|
public boolean | getImmediateFlush()Returns value of the ImmediateFlush option.
return immediateFlush;
|
public boolean | requiresLayout()The WriterAppender requires a layout. Hence, this method returns
true .
return true;
|
protected void | reset()Clear internal references to the writer and other variables.
Subclasses can override this method for an alternate closing
behavior.
closeWriter();
this.qw = null;
//this.tp = null;
|
public void | setEncoding(java.lang.String value)
encoding = value;
|
public synchronized void | setErrorHandler(org.apache.log4j.spi.ErrorHandler eh)Set the {@link ErrorHandler} for this WriterAppender and also the
underlying {@link QuietWriter} if any.
if(eh == null) {
LogLog.warn("You have tried to set a null error-handler.");
} else {
this.errorHandler = eh;
if(this.qw != null) {
this.qw.setErrorHandler(eh);
}
}
|
public void | setImmediateFlush(boolean value)If the ImmediateFlush option is set to
true , the appender will flush at the end of each
write. This is the default behavior. If the option is set to
false , then the underlying stream can defer writing
to physical medium to a later time.
Avoiding the flush operation at the end of each append results in
a performance gain of 10 to 20 percent. However, there is safety
tradeoff involved in skipping flushing. Indeed, when flushing is
skipped, then it is likely that the last few log events will not
be recorded on disk when the application exits. This is a high
price to pay even for a 20% performance gain.
immediateFlush = value;
|
public synchronized void | setWriter(java.io.Writer writer)Sets the Writer where the log output will go. The
specified Writer must be opened by the user and be
writable.
The java.io.Writer will be closed when the
appender instance is closed.
WARNING: Logging to an unopened Writer will fail.
reset();
this.qw = new QuietWriter(writer, errorHandler);
//this.tp = new TracerPrintWriter(qw);
writeHeader();
|
protected void | subAppend(org.apache.log4j.spi.LoggingEvent event)Actual writing occurs here.
Most subclasses of WriterAppender will need to
override this method.
this.qw.write(this.layout.format(event));
if(layout.ignoresThrowable()) {
String[] s = event.getThrowableStrRep();
if (s != null) {
int len = s.length;
for(int i = 0; i < len; i++) {
this.qw.write(s[i]);
this.qw.write(Layout.LINE_SEP);
}
}
}
if(this.immediateFlush) {
this.qw.flush();
}
|
protected void | writeFooter()Write a footer as produced by the embedded layout's {@link
Layout#getFooter} method.
if(layout != null) {
String f = layout.getFooter();
if(f != null && this.qw != null) {
this.qw.write(f);
this.qw.flush();
}
}
|
protected void | writeHeader()Write a header as produced by the embedded layout's {@link
Layout#getHeader} method.
if(layout != null) {
String h = layout.getHeader();
if(h != null && this.qw != null)
this.qw.write(h);
}
|