I'm sure this kind of class has come up before, but I thought I'd mention
it, and it's obvious partners of Log4jWriter, Log4jReader and
Log4jInputStream. I wrote it as a simple example of how Log4j can be
useful and figured I might as well send it to the list.

Another option is to use a TeeOutputStream and write an
OutputStreamRenderer. But the TeeOutputStream would have to take a
Category argument and then it would no longer be a normal TeeOutputStream.

Bay
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

package com.generationjava.log4j;

import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Category;

// snoop an outputstream
public class Log4jOutputStream extends FilterOutputStream {

    private Category logger;
    private int bufferChar;  // make this settable
    private ByteArrayOutputStream baos;

    public Log4jOutputStream(Category logger, OutputStream proxy) {
        super(proxy);
        this.logger = logger;
        this.baos = new ByteArrayOutputStream();
        this.bufferChar = (int)'\n';
    }

    public void write(int val) throws IOException {
        baos.write(val);
        if(val == this.bufferChar) {
            logger.debug(baos.toString());
            baos.reset();
        }
        super.write(val);
    }

    public void flush() throws IOException {
        logger.debug(baos.toString());
        baos.reset();
        super.flush();
    }

}



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to