DEBUG AN OUTPUTSTREAM WITH LOG4J Would you like to watch values being passed through an output stream without interfering with the code? The Apache Jakarta Log4j, currently the logging application program interface (API) of choice, allows you to set up debug and error logging without a significant impact on the system. The majority of the system works through the org.apache.log4j.Category class. http://jakarta.apache.org/log4j/docs/index.html By blending the Category class with a FilterOutputStream, you can watch the output of an OutputStream. Here's how: package com.generationjava.log4j; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.log4j.Category; public class Log4jOutputStream extends FilterOutputStream { private Category logger; public Log4jOutputStream(Category logger, OutputStream proxy) { super(proxy); this.logger = logger; } public void write(int val) throws IOException { char ch = (char)val; logger.debug(String.valueOf(ch)); super.write(val); } } Similarly, you can debug an InputStream, Reader, or Writer. The logging stream provides a simple example of how to chain the streams: public class LogOutput { static public Category logger = Category.getInstance(LogOutput.class); static public void main(String[] args) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Log4jOutputStream l4jos = new Log4jOutputStream(logger, baos); l4jos.write("This is a test. ".getBytes()); } } This code prints out the following: DEBUG (LogOutput) - T DEBUG (LogOutput) - h DEBUG (LogOutput) - i DEBUG (LogOutput) - s DEBUG (LogOutput) - DEBUG (LogOutput) - i DEBUG (LogOutput) - s DEBUG (LogOutput) - DEBUG (LogOutput) - a DEBUG (LogOutput) - DEBUG (LogOutput) - t DEBUG (LogOutput) - e DEBUG (LogOutput) - s DEBUG (LogOutput) - t DEBUG (LogOutput) - . DEBUG (LogOutput) - Here are the subsequent log4j.properties: log4j.rootCategory=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p (%c) - %m%n Finally, you must log per character and buffer each write until a newline is written. The flush( ) method will also need to be overridden to ensure that the buffer is properly emptied when the stream is flushed or closed. ----------------------------------------