org.apache.avalon.framework.logger

Class LoggerAwareOutputStream

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
ByteArrayOutputStreambos
Message buffer
protected Loggerm_logger
Logger reference
Constructor Summary
LoggerAwareOutputStream(Logger logger)
Constructor, creates instance of class.
Method Summary
voidclose()
Purposely flushes the stream, but doesn't close anything since the logger is managed by another class.
voidflush()
Flushes this output stream, writing any buffered content to the log
protected abstract voidlogMessage(String message)
Writes the message to the log.
voidwrite(int b)
Writes a byte to the internal buffer.

Field Detail

bos

private final ByteArrayOutputStream bos
Message buffer

m_logger

protected final Logger m_logger
Logger reference

Constructor Detail

LoggerAwareOutputStream

public LoggerAwareOutputStream(Logger logger)
Constructor, creates instance of class.

Parameters: logger logger this output stream should use

Method Detail

close

public void close()
Purposely flushes the stream, but doesn't close anything since the logger is managed by another class.

Throws: IOException if an IO error occurs

See Also: java.io.OutputStream#close()

flush

public void flush()
Flushes this output stream, writing any buffered content to the log

Throws: IOException on error

See Also: java.io.OutputStream#flush()

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

write

public void write(int b)
Writes a byte to the internal buffer. If a newline character is encountered, then the buffer is sent to the logger.

Parameters: b character to write

Throws: IOException if an error occurs

See Also: java.io.OutputStream#write(int)