org.apache.oodt.commons.io
Class WriterLogger

java.lang.Object
  extended by org.apache.oodt.commons.io.WriterLogger
All Implemented Interfaces:
PropertyChangeListener, EventListener, LogListener
Direct Known Subclasses:
FileLogger, ScreenLogger

public class WriterLogger
extends Object
implements LogListener

Log messages to a Writer. This class defines a LogListener that logs its messages to a character stream Writer. This formats and writes to the output stream all log events passed to LogListener.messageLogged(org.apache.oodt.commons.io.LogEvent), one per line. Each line is separated with the system's line separator characters, specified by the line.separator system property.

It ignores all events passed to LogListener.streamStarted(org.apache.oodt.commons.io.LogEvent) and LogListener.streamStopped(org.apache.oodt.commons.io.LogEvent).

It quietly hides all IO errors that may occur during writing.

Author:
Kelly
See Also:
Log

Field Summary
protected  boolean autoFlush
          If true, flush after every message logged.
protected  String lineSep
          What the line separator is.
protected  Writer writer
          The writer to which we write log messages.
 
Constructor Summary
WriterLogger(OutputStream outputStream)
          Constructor given an output stream.
WriterLogger(Writer writer)
          Constructor given a writer.
WriterLogger(Writer writer, boolean autoFlush)
          General constructor.
 
Method Summary
 void close()
          Close the writer.
protected  String formatMessage(Date timestamp, String source, Object category, String message)
          Format a message for logging.
 void messageLogged(LogEvent event)
          Log a message to the writer.
 void propertyChange(PropertyChangeEvent ignore)
           
 void streamStarted(LogEvent event)
          Ignore the stream started event.
 void streamStopped(LogEvent event)
          Ignore the stream stopped event.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

writer

protected Writer writer
The writer to which we write log messages. If null, then we were closed. This is protected so you can extend this class and log other events normally ignored.


autoFlush

protected boolean autoFlush
If true, flush after every message logged. This is protected so you can extend this class and log other events normally ignored.


lineSep

protected String lineSep
What the line separator is. This is protected so you can extend this class and log other events normally ignored.

Constructor Detail

WriterLogger

public WriterLogger(OutputStream outputStream)
Constructor given an output stream. Construct a writer logger that writes message events to the given output stream and flushes the stream after every logged message.

Parameters:
outputStream - The output stream to which to write events.

WriterLogger

public WriterLogger(Writer writer)
Constructor given a writer. Construct a writer logger that writes message events to the given writer and flushes the stream after every logged message.

Parameters:
writer - The writer to which to write events.

WriterLogger

public WriterLogger(Writer writer,
                    boolean autoFlush)
General constructor. Construct a writer logger that writes message events to the given writer and optionally flushes the stream after every logged message.

Parameters:
writer - The writer to which to write events.
autoFlush - If true, call flush on the writer after every message logged. If false, don't call flush.
Method Detail

close

public final void close()
Close the writer. This closes the writer to which this logger was logging. Future message events are ignored and not written.


messageLogged

public final void messageLogged(LogEvent event)
Log a message to the writer. This method first calls formatMessage(java.util.Date, java.lang.String, java.lang.Object, java.lang.String) to format the message, and then writes the message to the output stream.

Specified by:
messageLogged in interface LogListener
Parameters:
event - The event describing the message that was logged.

formatMessage

protected String formatMessage(Date timestamp,
                               String source,
                               Object category,
                               String message)
Format a message for logging. This method formats a message as follows:

epochTime (time) source category: message

where epochTime is the time in milliseconds since midnight, 1st January 1970 GMT, time is human-readable time, source is the source of the message, category is the category under which the message was logged, and message is the message.

You can override this method and provide your own formatting.

Parameters:
timestamp - Timestamp for the message.
source - Source of the message.
category - Category of the message.
message - The message text.


streamStarted

public void streamStarted(LogEvent event)
Ignore the stream started event.

Specified by:
streamStarted in interface LogListener
Parameters:
event - The event to ignore.

streamStopped

public void streamStopped(LogEvent event)
Ignore the stream stopped event.

Specified by:
streamStopped in interface LogListener
Parameters:
event - The event to ignore.

propertyChange

public void propertyChange(PropertyChangeEvent ignore)
Specified by:
propertyChange in interface PropertyChangeListener


Copyright © 1999-2013
Apache OODT. All Rights Reserved.