I was in the need to redirect the RMI logging output to log4j and created a 
small adapter that handles that, which is an OutputStream that transact to 
the Log4J loggers upon reception of new line character, flush or close.

Usage;

        StreamAdapter sa = new StreamAdapter( logger , level );
        RemoteServer.setLog( sa );


This is released as BeerWare;
"This license grant you to use the software in any way you want, no 
limitations, provided;
a) You hold the author unresponsible for any and all effects.
b) Drink a beer to salute the author.
"

;o)

package com.ewarna.lz;

import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.Priority;

/** Adapter for Streams to be directed to log4j Loggers.
 *
 * <p>This project is using this to redirect the
 * RMI Exception trace to Logger outputs, instead of the
 * System.out/System.err (whichever it may be). However,
 * this adapter is versatile enough to make any kind
 * of OutputStream content to go to the Logger system.</p>
 *
 **/
public class StreamAdapter extends OutputStream
{
    private static final char NEWLINE;
    private Logger m_Logger;
    private Priority m_Priority;
    private StringBuffer m_Message;

    static
    {
        NEWLINE = System.getProperty( "line.separator" ).charAt(0);
    }

    /** Creates the default StreamAdapter.
     *<p>Convinience constructor for
     * <code>StreamAdapter( Logger.getLogger( StreamAdapter.class ),
     * Level.INFO );</code>
     **/
    public StreamAdapter()
    {
        this( Logger.getLogger( StreamAdapter.class ), Level.ERROR );
    }

    /** Creates a StreamAdapter connecting to the given logger.
     *<p>Convinience constructor for
     * <code>StreamAdapter( logger,
     * Level.INFO );</code>
     **/
    public StreamAdapter( Logger logger )
    {
        this( logger, Level.INFO );
    }

    /** Creates a StreamAdapter connecting to a particular
     * logger with a message of a particular priority/level.
     */
    public StreamAdapter( Logger logger, Priority priority )
    {
        m_Logger = logger;
        m_Priority = priority;
        m_Message = new StringBuffer(200);
    }

    public void write( int b )
    {
        if( m_Logger != null && b == NEWLINE )
            flush();
        else
            m_Message.append( (char) b );
    }

    /** Flushes the outputstream and sends the buffered bytes
     * to its destination.
     * <p> This causes the call to the <code>Logger.log</code>
     * method, and will hence constitute a transaction in that
     * system. This method is called whenever a new line character
     * is encountered in the write stream, and upon close.
     **/
    public void flush()
    {
        if( m_Message.length() > 0 )
        {
            m_Logger.log( m_Priority, m_Message.toString() );
            m_Message.setLength(0);
        }
    }

    public void close()
    {
        flush();
    }

    public Logger getLogger()
    {
        return m_Logger;
    }

    public void setLogger( Logger logger )
    {
        m_Logger = logger;
    }

    public void setPriority( Priority priority )
    {
        m_Priority = priority;
    }

    public Priority getPriority()
    {
        return m_Priority;
    }

}

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

Reply via email to