Logback API
Version 0.9.8

ch.qos.logback.core
Class WriterAppender<E>

java.lang.Object
  extended by ch.qos.logback.core.spi.ContextAwareBase
      extended by ch.qos.logback.core.AppenderBase<E>
          extended by ch.qos.logback.core.WriterAppender<E>
All Implemented Interfaces:
Appender<E>, ContextAware, FilterAttachable, LifeCycle
Direct Known Subclasses:
ConsoleAppender, FileAppender

public class WriterAppender<E>
extends AppenderBase<E>

WriterAppender appends events to a hava.io.Writer. This class provides basic services that other appenders build upon. For more information about this appender, please refer to the online manual at http://logback.qos.ch/manual/appenders.html#WriterAppender

Author:
Ceki Gülcü

Field Summary
protected  String encoding
          The encoding to use when opening an InputStream.
protected  boolean immediateFlush
          Immediate flush means that the underlying writer or output stream will be flushed at the end of each append operation.
protected  Layout<E> layout
          The layout variable does not need to be set if the appender implementation has its own layout.
protected  Writer writer
          This is the Writer where we will write to.
 
Fields inherited from class ch.qos.logback.core.AppenderBase
name, started
 
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
 
Constructor Summary
WriterAppender()
          The default constructor does nothing.
 
Method Summary
protected  void append(E eventObject)
           
protected  void closeWriter()
          Close the underlying Writer.
protected  OutputStreamWriter createWriter(OutputStream os)
          Returns an OutputStreamWriter when passed an OutputStream.
 String getEncoding()
           
 boolean getImmediateFlush()
          Returns value of the ImmediateFlush option.
 Layout<E> getLayout()
          Returns the layout of this appender.
 void setEncoding(String value)
           
 void setImmediateFlush(boolean value)
          If the ImmediateFlush option is set to true, the appender will flush at the end of each write.
 void setLayout(Layout<E> layout)
          Set the layout for this appender.
 void setWriter(Writer writer)
           Sets the Writer where the log output will go.
 void start()
          Checks that requires parameters are set and if everything is in order, activates this appender.
 void stop()
          Stop this appender instance.
protected  void subAppend(E event)
          Actual writing occurs here.
 
Methods inherited from class ch.qos.logback.core.AppenderBase
addFilter, clearAllFilters, doAppend, getFilterChainDecision, getFirstFilter, getName, isStarted, setName, toString
 
Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getStatusManager, setContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
 

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. Immediate flush is slower but ensures that each append request is actually written. If immediateFlush is set to false, 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 immediateFlush variable is set to true by default.


encoding

protected String encoding
The encoding to use when opening an InputStream.

The encoding variable is set to null by default which results in the use of the system's default encoding.


writer

protected Writer writer
This is the Writer where we will write to.


layout

protected Layout<E> layout
The layout variable does not need to be set if the appender implementation has its own layout.

Constructor Detail

WriterAppender

public WriterAppender()
The default constructor does nothing.

Method Detail

setImmediateFlush

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.


getImmediateFlush

public boolean getImmediateFlush()
Returns value of the ImmediateFlush option.


start

public void start()
Checks that requires parameters are set and if everything is in order, activates this appender.

Specified by:
start in interface LifeCycle
Overrides:
start in class AppenderBase<E>

append

protected void append(E eventObject)
Specified by:
append in class AppenderBase<E>

stop

public void stop()
Stop this appender instance. The underlying stream or writer is also closed.

Stopped appenders cannot be reused.

Specified by:
stop in interface LifeCycle
Overrides:
stop in class AppenderBase<E>

closeWriter

protected void closeWriter()
Close the underlying Writer.


createWriter

protected OutputStreamWriter createWriter(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.


getEncoding

public String getEncoding()

setEncoding

public void setEncoding(String value)

setLayout

public void setLayout(Layout<E> layout)
Set the layout for this appender. Note that some appenders have their own (fixed) layouts or do not use any.

Specified by:
setLayout in interface Appender<E>
Overrides:
setLayout in class AppenderBase<E>

getLayout

public Layout<E> getLayout()
Returns the layout of this appender. The value may be null.

Specified by:
getLayout in interface Appender<E>
Overrides:
getLayout in class AppenderBase<E>

setWriter

public void setWriter(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.

Parameters:
writer - An already opened Writer.

subAppend

protected void subAppend(E event)
Actual writing occurs here.

Most subclasses of WriterAppender will need to override this method.

Since:
0.9.0

Logback API
Version 0.9.8

Copyright © 2005-2008 QOS.ch. All Rights Reserved.