Hi All,

        I've written a new logkit target for review, which logs events
        via email. The new target is attached, as SMTPOutputTarget.
        It requires the javamail jar file available from
        http://java.sun.com/products/javamail/index.html.

        If you have any comments, etc, feel free to fire away. If
        there's no major problems and it's ok with everyone, I'll check
        the source file into logkit later on during the week.

        I'm also working on an SMSOutputTarget which should be working
        soon to. I'll post it too as soon as it's ready.

        Cheers,

        Marcus
-- 
        .....
     ,,$$$$$$$$$,      Marcus Crafter
    ;$'      '$$$$:    Computer Systems Engineer
    $:         $$$$:   ManageSoft GmbH
     $       o_)$$$:   82-84 Mainzer Landstrasse
     ;$,    _/\ &&:'   60327 Frankfurt Germany
       '     /( &&&
           \_&&&&'
          &&&&.
    &&&&&&&:
/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 */
package org.apache.logkit.output.net;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log.output.AbstractOutputTarget;
import org.apache.log.format.Formatter;

/**
 * Logkit output target that logs data via SMTP (ie. email).
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Marcus Crafter</a>
 * @version CVS $Id:$
 */
public class SMTPOutputTarget extends AbstractOutputTarget
{
    // Mail session
    private Session m_session;

    // Message to be sent
    private Message m_message;

    // Address to sent mail to
    private Address[] m_toAddresses;

    // Address to mail is to be listed as sent from
    private Address m_fromAddress;

    // Mail subject
    private String m_subject;

    // Current size of mail, in units of log events
    private int m_msgSize;

    // Maximum size of mail, in units of log events
    private int m_maxMsgSize;

    // Buffer containing current mail
    private StringBuffer m_buffer;

    /**
     * SMTPOutputTarget constructor, creates a logkit output target
     * capable of logging to an SMTP (ie. email) target.
     *
     * @param toAddresses address logs should be sent to
     * @param fromAddress address logs should be sent from
     * @param subject subject logs should use
     * @param smtpHost SMTP host to use to send log mails
     * @param maxMsgSize maximum size of any log mail, in units of log events
     * @param formatter log formatter to use
     */
    public SMTPOutputTarget(
        final Address[] toAddresses,
        final Address fromAddress,
        final String subject,
        final String smtpHost,
        final int maxMsgSize,
        final Formatter formatter
    )
    {
        super(formatter);

        // setup log target
        m_maxMsgSize = maxMsgSize;
        m_toAddresses = toAddresses;
        m_fromAddress = fromAddress;
        m_subject = subject;

        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);
        m_session = Session.getDefaultInstance(props);

        // ready for business
        open();
    }

    /**
     * Method to write data to the log target. Logging data is stored in 
     * an internal buffer until the size limit is reached. When this happens
     * the data is sent to the SMTP target, and the buffer is reset for
     * subsequent events.
     *
     * @param data logging data to be written to target
     */
    protected void write( final String data )
    {
        try
        {
            // ensure we have a message object available
            if (m_message == null)
            {
                m_message = new MimeMessage(m_session);
                m_message.setFrom(m_fromAddress);
                m_message.setRecipients(Message.RecipientType.TO, m_toAddresses);
                m_message.setSubject(m_subject);
                m_message.setSentDate(new Date());
                m_msgSize = 0;
                m_buffer = new StringBuffer();
            }

            // add the data to the buffer, separated by a newline
            m_buffer.append(data);
            m_buffer.append('\n');
            ++m_msgSize;

            // send mail if message size has reached it's size limit
            if (m_msgSize >= m_maxMsgSize)
                send();
        }
        catch (MessagingException e)
        {
            getErrorHandler().error("Error creating message", e, null);
        }
    }

    /**
     * Closes this log target. Sends currently buffered message, if existing.
     */
    public synchronized void close()
    {
        super.close();
        send();
    }

    /**
     * Method to enable/disable debugging on the javamail session.
     *
     * @param flag true to enable debugging, false to disable it
     */
    public void setDebug(boolean flag)
    {
        m_session.setDebug(flag);               
    }

    /**
     * Helper method to send the currently buffered message,
     * if existing.
     */
    private void send()
    {
        try
        {
            if (m_message != null && m_buffer != null)
            {
                m_message.setText(m_buffer.toString());
                Transport.send(m_message);
                m_message = null;
            }
        }
        catch (MessagingException e)
        {
            getErrorHandler().error("Error sending message", e, null);
        }
    }
}

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

Reply via email to