星期五, 五月 28, 2010

如何在Java Mail中是用Log4j来记录Java Mail Debug 信息?的

Java Mail 的debug信息非常丰富,包含了同服务器完整的协议过程,
但是这个debug是直接输出到System.out的,需要用 >>来截获。

不过Java Mail 提供了setDebugOut(PrintStream)的接口,
就用这个可以实现 Java Mail和 log4j的联合使用

首先编写一个PrintStream的类做一个Bridge 同  Log4j


package com.email;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.*;
/**
 *
 * @author U253MIS038
 */
public class LoggingOutputStream extends OutputStream
{

    protected boolean hasBeenClosed = false;

    protected byte[] buf;

    protected int count;

    private int bufLength;

    public static final int DEFAULT_BUFFER_LENGTH = 2048;

    protected Logger logger;

    protected Level level;

    private LoggingOutputStream()
    {

    }



    public LoggingOutputStream(Logger log, Level level)
            throws IllegalArgumentException {
        if (log == null) {
            throw new IllegalArgumentException("cat == null");
        }
        if (level == null) {
            throw new IllegalArgumentException("priority == null");
        }

        this.level = level;
        logger = log;
        bufLength = DEFAULT_BUFFER_LENGTH;
        buf = new byte[DEFAULT_BUFFER_LENGTH];
        count = 0;
    }



    public void close()
    {
        flush();
        hasBeenClosed = true;
    }



    public void write(final int b) throws IOException
    {
        if (hasBeenClosed) {
            throw new IOException("The stream has been closed.");
        }

        if (count == bufLength)
        {
            final int newBufLength = bufLength + DEFAULT_BUFFER_LENGTH;
            final byte[] newBuf = new byte[newBufLength];

            System.arraycopy(buf, 0, newBuf, 0, bufLength);

            buf = newBuf;
            bufLength = newBufLength;
        }

        buf[count] = (byte) b;
        count++;
    }



    public void flush()
    {
        if (count == 0)
        {
            return;
        }


        if (count == 1 && ((char) buf[0]) == '\n')
        {
            reset();
            return;
        }
        final byte[] theBytes = new byte[buf.length];
        System.arraycopy(buf, 0, theBytes, 0, count);
        logger.log(level, new String(theBytes));
        reset();
    }


    private void reset()
    {
             count = 0;
    }

}


2,然后再调用Java Mail中使用如下方法实现了定向

            static Logger log  = Logger.getLogger( email.class );
            Properties props = System.getProperties();
               
            config = new PropertiesConfiguration(PROP_FILE);
            String mailip = config.getString("EMAIL_SERVER_IP");       
            props.put("mail.smtp.host", mailip);

            Session session = Session.getInstance(props, null);
            session.setDebugOut(new PrintStream(new  LoggingOutputStream(Logger.getRootLogger(),Level.DEBUG), true));
                        session.setDebug(enableDebug);