Class LoggerAwareOutputStream

java.lang.Object
java.io.OutputStream
org.apache.avalon.framework.logger.LoggerAwareOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public abstract class LoggerAwareOutputStream extends OutputStream
Logger aware output stream, characters written to this OutputStream are buffered until a newline character is encountered, or a flush() is called.

Extend to specify the log method that the message should be invoked. eg:

 setOutputStream( new LoggerAwareOutputStream( getLogger() ) {
     protected void logMessage( String message )
     {
         if ( m_logger.isDebugEnabled() )
         {
             m_logger.debug( message );
         }
     }
 } );
 
Since:
Nov 19, 2004 7:03:50 PM
Version:
$Revision:$
Author:
Marcus Crafter
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final ByteArrayOutputStream
    Message buffer
    protected final org.apache.avalon.framework.logger.Logger
    Logger reference
  • Constructor Summary

    Constructors
    Constructor
    Description
    LoggerAwareOutputStream(org.apache.avalon.framework.logger.Logger logger)
    Constructor, creates instance of class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Purposely flushes the stream, but doesn't close anything since the logger is managed by another class.
    void
    Flushes this output stream, writing any buffered content to the log
    protected abstract void
    logMessage(String message)
    Writes the message to the log.
    void
    write(int b)
    Writes a byte to the internal buffer.

    Methods inherited from class java.io.OutputStream

    nullOutputStream, write, write

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • bos

      private final ByteArrayOutputStream bos
      Message buffer
    • m_logger

      protected final org.apache.avalon.framework.logger.Logger m_logger
      Logger reference
  • Constructor Details

    • LoggerAwareOutputStream

      public LoggerAwareOutputStream(org.apache.avalon.framework.logger.Logger logger)
      Constructor, creates instance of class.
      Parameters:
      logger - logger this output stream should use
  • Method Details

    • write

      public void write(int b) throws IOException
      Writes a byte to the internal buffer. If a newline character is encountered, then the buffer is sent to the logger.
      Specified by:
      write in class OutputStream
      Parameters:
      b - character to write
      Throws:
      IOException - if an error occurs
      See Also:
    • flush

      public void flush() throws IOException
      Flushes this output stream, writing any buffered content to the log
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - on error
      See Also:
    • close

      public void close() throws IOException
      Purposely flushes the stream, but doesn't close anything since the logger is managed by another class.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - if an IO error occurs
      See Also:
    • logMessage

      protected abstract void logMessage(String message)
      Writes the message to the log. Subclasses should override this method to send the message to the log level they require.
      Parameters:
      message - message to be written