Class WriterAppender
- java.lang.Object
-
- org.apache.log4j.AppenderSkeleton
-
- org.apache.log4j.WriterAppender
-
- All Implemented Interfaces:
Appender,OptionHandler
- Direct Known Subclasses:
ConsoleAppender,FileAppender
public class WriterAppender extends AppenderSkeleton
WriterAppender appends log events to aWriteror anOutputStreamdepending on the user's choice.- Since:
- 1.1
- Author:
- Ceki Gülcü
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.StringencodingThe encoding to use when writing.protected booleanimmediateFlushImmediate flush means that the underlying writer or output stream will be flushed at the end of each append operation unless shouldFlush() is overridden.protected QuietWriterqwThis is thequietWriterwhere we will write to.-
Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold
-
-
Constructor Summary
Constructors Constructor Description WriterAppender()This default constructor does nothing.WriterAppender(Layout layout, java.io.OutputStream os)Instantiate a WriterAppender and set the output destination to a newOutputStreamWriterinitialized withosas itsOutputStream.WriterAppender(Layout layout, java.io.Writer writer)Instantiate a WriterAppender and set the output destination towriter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivateOptions()Does nothing.voidappend(LoggingEvent event)This method is called by theAppenderSkeleton.doAppend(org.apache.log4j.spi.LoggingEvent)method.protected booleancheckEntryConditions()This method determines if there is a sense in attempting to append.voidclose()Close this appender instance.protected voidcloseWriter()Close the underlyingWriter.protected java.io.OutputStreamWritercreateWriter(java.io.OutputStream os)Returns an OutputStreamWriter when passed an OutputStream.java.lang.StringgetEncoding()booleangetImmediateFlush()Returns value of the ImmediateFlush option.booleanrequiresLayout()The WriterAppender requires a layout.protected voidreset()Clear internal references to the writer and other variables.voidsetEncoding(java.lang.String value)voidsetErrorHandler(ErrorHandler eh)Set theErrorHandlerfor this WriterAppender and also the underlyingQuietWriterif any.voidsetImmediateFlush(boolean value)If the ImmediateFlush option is set totrue, the appender will flush at the end of each write.voidsetWriter(java.io.Writer writer)Sets the Writer where the log output will go.protected booleanshouldFlush(LoggingEvent event)Determines whether the writer should be flushed after this event is written.protected voidsubAppend(LoggingEvent event)Actual writing occurs here.protected voidwriteFooter()Write a footer as produced by the embedded layout'sLayout.getFooter()method.protected voidwriteHeader()Write a header as produced by the embedded layout'sLayout.getHeader()method.-
Methods inherited from class org.apache.log4j.AppenderSkeleton
addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setLayout, setName, setThreshold
-
-
-
-
Field Detail
-
immediateFlush
protected boolean immediateFlush
Immediate flush means that the underlying writer or output stream will be flushed at the end of each append operation unless shouldFlush() is overridden. Immediate flush is slower but ensures that each append request is actually written. IfimmediateFlushis set tofalse, then there is a good chance that the last few logs events are not actually written to persistent media if and when the application crashes.The
immediateFlushvariable is set totrueby default.
-
encoding
protected java.lang.String encoding
The encoding to use when writing.The
encodingvariable is set tonull by default which results in the utilization of the system's default encoding.
-
qw
protected QuietWriter qw
This is thequietWriterwhere we will write to.
-
-
Constructor Detail
-
WriterAppender
public WriterAppender()
This default constructor does nothing.
-
WriterAppender
public WriterAppender(Layout layout, java.io.OutputStream os)
Instantiate a WriterAppender and set the output destination to a newOutputStreamWriterinitialized withosas itsOutputStream.
-
WriterAppender
public WriterAppender(Layout layout, java.io.Writer writer)
Instantiate a WriterAppender and set the output destination towriter.The
writermust have been previously opened by the user.
-
-
Method Detail
-
setImmediateFlush
public void setImmediateFlush(boolean value)
If the ImmediateFlush option is set totrue, the appender will flush at the end of each write. This is the default behavior. If the option is set tofalse, 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.
-
getImmediateFlush
public boolean getImmediateFlush()
Returns value of the ImmediateFlush option.
-
activateOptions
public void activateOptions()
Does nothing.- Specified by:
activateOptionsin interfaceOptionHandler- Overrides:
activateOptionsin classAppenderSkeleton
-
append
public void append(LoggingEvent event)
This method is called by theAppenderSkeleton.doAppend(org.apache.log4j.spi.LoggingEvent)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.
- Specified by:
appendin classAppenderSkeleton
-
checkEntryConditions
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
falseis returned.
-
close
public void close()
Close this appender instance. The underlying stream or writer is also closed.Closed appenders cannot be reused.
- Since:
- 0.8.4
- See Also:
setWriter(java.io.Writer)
-
closeWriter
protected void closeWriter()
Close the underlyingWriter.
-
createWriter
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 theencodingproperty. 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.
-
getEncoding
public java.lang.String getEncoding()
-
setEncoding
public void setEncoding(java.lang.String value)
-
setErrorHandler
public void setErrorHandler(ErrorHandler eh)
Set theErrorHandlerfor this WriterAppender and also the underlyingQuietWriterif any.- Specified by:
setErrorHandlerin interfaceAppender- Overrides:
setErrorHandlerin classAppenderSkeleton
-
setWriter
public 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.Writerwill be closed when the appender instance is closed.WARNING: Logging to an unopened Writer will fail.
- Parameters:
writer- An already opened Writer.
-
subAppend
protected void subAppend(LoggingEvent event)
Actual writing occurs here.Most subclasses of
WriterAppenderwill need to override this method.- Since:
- 0.9.0
-
requiresLayout
public boolean requiresLayout()
The WriterAppender requires a layout. Hence, this method returnstrue.
-
reset
protected void reset()
Clear internal references to the writer and other variables.Subclasses can override this method for an alternate closing behavior.
-
writeFooter
protected void writeFooter()
Write a footer as produced by the embedded layout'sLayout.getFooter()method.
-
writeHeader
protected void writeHeader()
Write a header as produced by the embedded layout'sLayout.getHeader()method.
-
shouldFlush
protected boolean shouldFlush(LoggingEvent event)
Determines whether the writer should be flushed after this event is written.- Since:
- 1.2.16
-
-