Hi

Since the .java files are being dropped, I am resubmitting the new RFC
Complient SyslogAppender as textfiles this time.

regards
Hermod
 <<RFCSyslogAppender.txt>>  <<RFCSyslogQuietWriter.txt>> 


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

This email with attachments is solely for the use of the individual or
entity to whom it is addressed. Please also be aware that DnB cannot
accept any payment orders or other legally binding correspondence with
customers as a part of an email. 

This email message has been virus checked by the virus programs used
in the DnB Group.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

/*
 * 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.log4j.net;

import org.apache.log4j.Layout;
import org.apache.log4j.helpers.RFCSyslogQuietWriter;
import org.apache.log4j.helpers.SyslogWriter;
import org.apache.log4j.spi.LoggingEvent;

/**
 * This Syslog appender is an extention of [EMAIL PROTECTED] SyslogAppender} that 
follows the rfc 
 * as given by : http://www.ietf.org/rfc/rfc3164.txt : <pri>Mmm dd hh:mm:ss host 
message
 * It is also has the option of using the extensions as defined on the Solaris 
platform :
 * <pri>Mmm dd hh:mm:ss host [ID n facility.level] message
 * 
 * @author <a href="[EMAIL PROTECTED]">Hermod Opstvedt, DnB</a>
 * @version 1.0
 */
public class RFCSyslogAppender extends SyslogAppender
{

        private RFCSyslogQuietWriter rsqw;

        private boolean solarisFormat = false;

        private int solarisMessageId = 0;

        /**
         * Default constructor
         */
        public RFCSyslogAppender()
        {
                super();
        }

        /**
         * @see org.apache.log4j.net.SyslogAppender#SyslogAppender(Layout, int)
         */
        public RFCSyslogAppender(Layout layout, int syslogFacility)
        {
                super(layout, syslogFacility);
        }

        /**
         * @see org.apache.log4j.net.SyslogAppender#SyslogAppender(Layout, String, int)
         */
        public RFCSyslogAppender(Layout layout, String syslogHost, int syslogFacility)
        {
                super(layout, syslogHost, syslogFacility);
        }

        /**
         * Method RFCSyslogAppender. Constructor
         * @param [EMAIL PROTECTED] org.apache.log4j.Layout} layout - The [EMAIL 
PROTECTED] org.apache.log4j.Layout} that is to be used
         * @param [EMAIL PROTECTED] java.lang.String} syslogHost - The name of the 
Host to send Syslog messages to
         * @param int syslogFacility - The Facility that is associated with the Syslog 
message
         * @param boolean solarisFormat - Whether or not Solaris 8++ messageids should 
be printed
         */
        public RFCSyslogAppender(Layout layout, String syslogHost, int syslogFacility, 
boolean solarisFormat)
        {
                super(layout, syslogHost, syslogFacility);
                this.solarisFormat=solarisFormat;
        }

        /**
         * Method RFCSyslogAppender. Constructor
         * @param [EMAIL PROTECTED] org.apache.log4j.Layout} layout - The [EMAIL 
PROTECTED] org.apache.log4j.Layout} that is to be used
         * @param [EMAIL PROTECTED] java.lang.String} syslogHost - The name of the 
Host to send Syslog messages to
         * @param int syslogFacility - The Facility that is associated with the Syslog 
message
         * @param boolean solarisFormat - Whether or not Solaris 8++ messageids should 
be printed
         * @param int solarisId - The Solaris 8++ messageid to use
         */
        public RFCSyslogAppender(Layout layout, String syslogHost, int syslogFacility, 
boolean solarisFormat, int solarisId)
        {
                super(layout, syslogHost, syslogFacility);
                this.solarisFormat=solarisFormat;
                this.solarisMessageId=solarisId;
        }

        /**
         * @see org.apache.log4j.net.SyslogAppender#setSyslogHost(String)
         */
        public void setSyslogHost(String syslogHost)
        {
                this.rsqw =
                        new RFCSyslogQuietWriter(
                                new SyslogWriter(syslogHost),
                                syslogFacility,
                                solarisFormat,
                                solarisMessageId,
                                errorHandler);
                this.syslogHost = syslogHost;
        }

        /**
         * @see org.apache.log4j.AppenderSkeleton#append(LoggingEvent)
         */
        public void append(LoggingEvent event)
        {

                if (!isAsSevereAsThreshold(event.getLevel()))
                        return;

                if (rsqw == null)
                {
                        errorHandler.error("No syslog host is set for Appender named 
\"" + this.name + "\".");
                        return;
                }

                String buffer = (facilityPrinting ? facilityStr : "") + 
layout.format(event);

                rsqw.setLevel(event.getLevel().getSyslogEquivalent());
                rsqw.write(buffer);

                String[] s = event.getThrowableStrRep();
                if (s != null)
                {
                        int len = s.length;
                        if (len > 0)
                        {
                                rsqw.write(s[0]);

                                for (int i = 1; i < len; i++)
                                {
                                        rsqw.write(TAB + s[i].substring(1));
                                }
                        }

                }
        }

        /**
         * Returns whether or not to add the Solaris 8++ messageid to the message
         * @return boolean
         */
        public boolean isSolarisFormat()
        {
                return solarisFormat;
        }

        /**
         * Sets whether or not to add the Solaris 8++ messageid to the message
         * Default false;
         * @param boolean solarisFormat The solarisFormat to set
         */
        public void setSolarisFormat(boolean solarisFormat)
        {
                this.solarisFormat = solarisFormat;
                if(rsqw!=null)
                {
                        rsqw.setSolarisFormat(solarisFormat);
                }
        }

        /**
         * Returns the Solaris 8++ messageid
         * @return int
         */
        public int getSolarisMessageId()
        {
                return solarisMessageId;
        }

        /**
         * Sets the Solaris 8++ messageid
         * @param int solarisId The solarisId to set
         */
        public void setSolarisMessageId(int solarisMessageId)
        {
                this.solarisMessageId = solarisMessageId;
                if (rsqw != null)
                {
                        this.rsqw.setSolarisMessageId(solarisMessageId);
                }
        }

        /**
         * @see org.apache.log4j.net.SyslogAppender#setFacility(String)
         */
        public void setFacility(String facilityName)
        {
                super.setFacility(facilityName);
                if (rsqw != null)
                {
                        this.rsqw.setSyslogFacility(syslogFacility);
                }
        }

}
/*
 * 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.log4j.helpers;

import java.io.Writer;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.log4j.net.SyslogAppender;
import org.apache.log4j.spi.ErrorHandler;

/**
 * This Syslog quietwriter is an extention of [EMAIL PROTECTED] SyslogWriter} that 
follows the rfc 
 * as given by : http://www.ietf.org/rfc/rfc3164.txt : <pri>Mmm dd hh:mm:ss host 
message
 * It is also has the option of using the extensions as defined on the Solaris 
platform :
 * <pri>Mmm dd hh:mm:ss host [ID n facility.level] message
 * where ID n is the Solaris messageid
 * 
 * @author <a href="[EMAIL PROTECTED]">Hermod Opstvedt, DnB</a>
 * @version 1.0
 */
public class RFCSyslogQuietWriter extends QuietWriter
{
        // Must use US locale to get the correct month abreviation      
        private SimpleDateFormat sdf = new SimpleDateFormat("MMM dd hh:mm:ss", new 
DateFormatSymbols(Locale.US));

        private boolean solarisFormat;
        private int syslogFacility;
        private int level;
        private int solarisMessageId;

        public static final int SYSLOG_EMERGENCY = 0;
        public static final int SYSLOG_ALERT = 1;
        public static final int SYSLOG_CRITICAL = 2;
        public static final int SYSLOG_ERROR = 3;
        public static final int SYSLOG_WARNING = 4;
        public static final int SYSLOG_NOTICE = 5;
        public static final int SYSLOG_INFORMATIONAL = 6;
        public static final int SYSLOG_DEBUG = 7;

        /**
         * Method RFCSyslogQuietWriter. Constructor
         * @param {link java.io.Writer} writer. The Writer to use.
         * @param int syslogFacility. The Syslog facility associated with the message
         * @param {link org.apache.log4j.spi.ErrorHandler} eh. The ErrorHandler to use
         */
        public RFCSyslogQuietWriter(Writer writer, int syslogFacility, ErrorHandler eh)
        {
                super(writer, eh);
                this.syslogFacility = syslogFacility;
        }

        /**
         * Method RFCSyslogQuietWriter. Constructor
         * @param {link java.io.Writer} writer. The Writer to use.
         * @param int syslogFacility - The Facility that is associated with the Syslog 
message
         * @param boolean solarisFormat - Whether or not Solaris 8++ messageids should 
be printed
         * @param {link org.apache.log4j.spi.ErrorHandler} eh. The ErrorHandler to use
         */
        public RFCSyslogQuietWriter(Writer writer, int syslogFacility, boolean 
solarisFormat, ErrorHandler eh)
        {
                this(writer, syslogFacility, solarisFormat, 0, eh);
        }

        /**
         * Method RFCSyslogQuietWriter. Constructor
         * @param {link java.io.Writer} writer. The Writer to use.
         * @param int syslogFacility - The Facility that is associated with the Syslog 
message
         * @param boolean solarisFormat - Whether or not Solaris 8++ messageids should 
be printed
         * @param int solarisId - The Solaris 8++ messageid to use
         * @param {link org.apache.log4j.spi.ErrorHandler} eh. The ErrorHandler to use
         */
        public RFCSyslogQuietWriter(Writer writer, int syslogFacility, boolean 
solarisFormat, int id, ErrorHandler eh)
        {
                super(writer, eh);
                this.syslogFacility = syslogFacility;
                this.solarisMessageId = id;
                this.solarisFormat = solarisFormat;
        }

        /**
         * This method prepends the message with the nessesary priority and header 
information
         * as pr. the RFC3164
         * @see java.io.Writer#write(String)
         */
        public void write(String string)
        {
                // Append the Solaris messageid if set
                if (solarisFormat)
                {
                        string = appendSolarisFormat(string);
                }

                // Add the priority and header as defined in RFC3164
                StringBuffer sb =
                        new StringBuffer("<").append(syslogFacility | 
level).append(">").append(getRFCHeader()).append(" ").append(
                                string);
                super.write(sb.toString());
        }

        /**
         * This method prepends the message with the optional Solaris 8++ messageid.
         * @param {link java.lang.String} string
         * @return String
         */
        public String appendSolarisFormat(String string)
        {
                StringBuffer temp =
                        new StringBuffer("[ID ")
                                .append(solarisMessageId)
                                .append(" ")
                                
.append(SyslogAppender.getFacilityString(syslogFacility))
                                .append(".")
                                .append(getSyslogLevelString(level))
                                .append("] ")
                                .append(string);
                return temp.toString();
        }

        /**
         * This method returns a text representation of the Syslog level
         * @param int level
         * @return String
         */
        public String getSyslogLevelString(int level)
        {
                switch (level)
                {
                        case SYSLOG_EMERGENCY :
                                return "emerg";
                        case SYSLOG_CRITICAL :
                                return "crit";
                        case SYSLOG_ALERT :
                                return "alert";
                        case SYSLOG_ERROR :
                                return "err";
                        case SYSLOG_INFORMATIONAL :
                                return "info";
                        case SYSLOG_NOTICE :
                                return "notice";
                        case SYSLOG_WARNING :
                                return "warning";
                        case SYSLOG_DEBUG :
                                return "debug";
                        default :
                                return null;
                }

        }

        /**
         * This method builds the RFC3164 header
         * @return String
         */
        public String getRFCHeader()
        {
                StringBuffer sb = new StringBuffer(sdf.format(new Date()));
                //According to the RFC the day of the month must be right justified 
with
                // no leading 0
                if (sb.charAt(4) == '0')
                {
                        sb.setCharAt(4, ' ');
                }
                sb.append(" ").append(getLocalHostname());
                return sb.toString();
        }

        /**
         * This method gets the network name of the machine we are running on.
         * Returns null if not defined.
         * @return String
         */
        public String getLocalHostname()
        {
                String hostname = null;
                try
                {
                        InetAddress addr = InetAddress.getLocalHost();

                        // Get IP Address
                        byte[] ipAddr = addr.getAddress();

                        // Get hostname
                        hostname = addr.getHostName();
                }
                catch (UnknownHostException e)
                {
                }

                return hostname;
        }

        /**
         * Sets whether or not to add the Solaris 8++ messageid to the message
         * Default false;
         * @param boolean solarisFormat The solarisFormat to set
         */
        public void setSolarisFormat(boolean solarisFormat)
        {
                this.solarisFormat = solarisFormat;
        }

        /**
         * Sets the level associated with the message.
         * @param int level. The level to set.
         */
        public void setLevel(int level)
        {
                this.level = level;
        }

        /**
         * Sets the syslog Facility associated with the message.
         * @param int syslogFacility. The syslogFacility to set
         */
        public void setSyslogFacility(int syslogFacility)
        {
                this.syslogFacility = syslogFacility;
        }

        /**
         * Sets the Solaris 8++ messageid associated with the message.
         * @param id The id to set
         */
        public void setSolarisMessageId(int solarisMessageId)
        {
                this.solarisMessageId = solarisMessageId;
        }

}

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

Reply via email to