You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2004/02/20 23:44:11 UTC

cvs commit: avalon/logging/logkit/plugins/syslog/src/java/org/apache/avalon/logging/logkit/factory/syslog Resources.properties SyslogTarget.java SyslogTargetFactory.java package.html

mcconnell    2004/02/20 14:44:11

  Added:       logging/logkit/plugins/syslog .cvsignore maven.xml
                        project.properties project.xml
               logging/logkit/plugins/syslog/src/java/org/apache/avalon/logging/logkit/factory/syslog
                        Resources.properties SyslogTarget.java
                        SyslogTargetFactory.java package.html
  Log:
  Syslog plugin target addition.
  
  Revision  Changes    Path
  1.1                  avalon/logging/logkit/plugins/syslog/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  maven.log
  velocity.log
  build.properties
  target
  .classpath
  .project
  
  
  
  1.1                  avalon/logging/logkit/plugins/syslog/maven.xml
  
  Index: maven.xml
  ===================================================================
  <project default="jar:install" xmlns:maven="jelly:maven" xmlns:j="jelly:core" xmlns:util="jelly:util" xmlns:ant="jelly:ant">
  
    <postGoal name="java:prepare-filesystem">
      <attainGoal name="avalon:artifact"/>
    </postGoal>
  
    <postGoal name="jar:install">
      <ant:copy verbose="yes"
         toDir="${maven.repo.local}/${pom.groupId}/jars" 
         file="${maven.build.dir}/${pom.artifactId}-${pom.currentVersion}.jar.meta" />
    </postGoal>
  
  </project>
  
  
  
  1.1                  avalon/logging/logkit/plugins/syslog/project.properties
  
  Index: project.properties
  ===================================================================
  #
  # factory class
  #
  
  avalon.artifact.factory = org.apache.avalon.logging.logkit.factory.syslog.SyslogTargetFactory
  
  
  
  
  
  1.1                  avalon/logging/logkit/plugins/syslog/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <project>
  
    <extend>${basedir}/../../../../project.xml</extend>
  
    <groupId>avalon-logging</groupId>
    <id>avalon-logkit-syslog</id>
    <name>Avalon Logging Logkit Syslog Target</name>
    <package>org.apache.avalon.logging.logkit</package>
    <currentVersion>1.0-SNAPSHOT</currentVersion>
  
    <inceptionYear>2002</inceptionYear>
    <shortDescription>Avalon Logging Logkit Syslog Target</shortDescription>
  
    <dependencies>
  
      <!-- avalon dependecies -->
  
      <dependency>
        <groupId>avalon-repository</groupId>
        <artifactId>avalon-repository-main</artifactId>
        <version>1.3-SNAPSHOT</version>
      </dependency>
  
      <dependency>
        <groupId>avalon-logging</groupId>
        <artifactId>avalon-logging-api</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>avalon-logging</groupId>
        <artifactId>avalon-logkit-api</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
  
      <dependency>
        <groupId>avalon-framework</groupId>
        <artifactId>avalon-framework-api</artifactId>
        <version>4.1.5</version>
      </dependency>
  
      <dependency>
        <id>excalibur-i18n</id>
        <version>1.1</version>
      </dependency>
  
      <dependency>
        <groupId>logkit</groupId>
        <artifactId>logkit</artifactId>
        <version>1.2.2</version>
      </dependency>
  
    </dependencies>
  </project>
  
  
  
  1.1                  avalon/logging/logkit/plugins/syslog/src/java/org/apache/avalon/logging/logkit/factory/syslog/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  syslog.error.missing-address=Missing 'address' attribute is supplied syslog target configuration.
  syslog.error.unknown-host=Syslog target references an unknown host.
  syslog.error.missing-host=Syslog target does not contain required 'hostname' attribute.
  syslog.error.missing-port=Syslog target does not contain required 'port' attribute.
  syslog.error.missing-facility=Syslog target does not contain required 'facility' attribute.
  syslog.error.internal=Internal error while attempting to construct syslog target.
  
  
  
  
  1.1                  avalon/logging/logkit/plugins/syslog/src/java/org/apache/avalon/logging/logkit/factory/syslog/SyslogTarget.java
  
  Index: SyslogTarget.java
  ===================================================================
  /* 
   * Copyright 2004 Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.avalon.logging.logkit.factory.syslog;
  
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  
  import org.apache.log.LogEvent;
  import org.apache.log.Priority;
  import org.apache.log.format.Formatter;
  import org.apache.log.output.AbstractOutputTarget;
  import org.apache.log.output.net.DatagramOutputTarget;
  
  /**
   * <p>Title: </p>
   * <p>Description: </p>
   * <p>Copyright: Copyright (c) 2002</p>
   * <p>Company: </p>
   * @author unascribed
   * @version 1.0
   */
  
  public class SyslogTarget extends DatagramOutputTarget 
  {
      // The following constants are extracted from a syslog.h file
      // copyrighted by the Regents of the University of California
      // I hope nobody at Berkley gets offended.
  
      /** Kernel messages */
      final static public int LOG_KERN     = 0;
      /** Random user-level messages */
      final static public int LOG_USER     = 1<<3;
      /** Mail system */
      final static public int LOG_MAIL     = 2<<3;
      /** System daemons */
      final static public int LOG_DAEMON   = 3<<3;
      /** security/authorization messages */
      final static public int LOG_AUTH     = 4<<3;
      /** messages generated internally by syslogd */
      final static public int LOG_SYSLOG   = 5<<3;
  
      /** line printer subsystem */
      final static public int LOG_LPR      = 6<<3;
      /** network news subsystem */
      final static public int LOG_NEWS     = 7<<3;
      /** UUCP subsystem */
      final static public int LOG_UUCP     = 8<<3;
      /** clock daemon */
      final static public int LOG_CRON     = 9<<3;
      /** security/authorization  messages (private) */
      final static public int LOG_AUTHPRIV = 10<<3;
      /** ftp daemon */
      final static public int LOG_FTP      = 11<<3;
  
      // other codes through 15 reserved for system use
      /** reserved for local use */
      final static public int LOG_LOCAL0 = 16<<3;
      /** reserved for local use */
      final static public int LOG_LOCAL1 = 17<<3;
      /** reserved for local use */
      final static public int LOG_LOCAL2 = 18<<3;
      /** reserved for local use */
      final static public int LOG_LOCAL3 = 19<<3;
      /** reserved for local use */
      final static public int LOG_LOCAL4 = 20<<3;
      /** reserved for local use */
      final static public int LOG_LOCAL5 = 21<<3;
      /** reserved for local use */
      final static public int LOG_LOCAL6 = 22<<3;
      /** reserved for local use*/
      final static public int LOG_LOCAL7 = 23<<3;
  
      final static public int SYSLOG_FATAL = 0;
      final static public int SYSLOG_ERROR = 3;
      final static public int SYSLOG_WARN  = 4;
      final static public int SYSLOG_INFO  = 6;
      final static public int SYSLOG_DEBUG = 7;
  
      private int facility = LOG_USER;
  
      /**
       * Create a output target with end point specified by address and port.
       *
       * @param address the address endpoint
       * @param port the address port
       * @exception IOException if an error occurs
       */
      public SyslogTarget( final InetAddress address,
                                   final int port,
                                   final Formatter formatter,
                                   final int facility )
          throws IOException
      {
          super(address, port, formatter);
          this.facility = facility;
      }
  
      /**
       * Create a output target with end point specified by address and port.
       *
       * @param address the address endpoint
       * @param port the address port
       * @exception IOException if an error occurs
       */
      public SyslogTarget( final InetAddress address,
                                   final int port,
                                   final Formatter formatter )
          throws IOException
      {
          super(address, port, formatter);
      }
  
      /**
       * Create a output target with end point specified by address and port.
       *
       * @param address the address endpoint
       * @param port the address port
       * @exception IOException if an error occurs
       */
      public SyslogTarget( final InetAddress address, final int port )
          throws IOException
      {
          super( address, port );
      }
  
      protected void doProcessEvent( LogEvent event )
      {
          int priority = event.getPriority().getValue();
          int syslogPriority = SYSLOG_INFO;
  
          switch (priority) {
              case 5 : {
                  syslogPriority = SYSLOG_DEBUG;
                  break;
              }
              case 10 : {
                  syslogPriority = SYSLOG_INFO;
                  break;
              }
              case 15 : {
                  syslogPriority = SYSLOG_WARN;
                  break;
              }
              case 20 : {
                  syslogPriority = SYSLOG_ERROR;
                  break;
              }
              case 25 : {
                  syslogPriority = SYSLOG_FATAL;
                  break;
              }
          }
  
          //final String data = format( event );
          if( null != m_formatter )
          {
              write ("<"+(facility | syslogPriority)+">"+m_formatter.format( event ));
          }
          else
          {
              write ("<"+(facility | syslogPriority)+">"+event.toString());
          }
      }
  
      public static int getFacilityValue (String name) 
      {
          if( name.equalsIgnoreCase( "kern" ) ) 
          {
              return LOG_KERN;
          } 
          else if( name.equalsIgnoreCase( "user" ) )  
          {
              return LOG_USER;
          } 
          else if( name.equalsIgnoreCase("mail") ) 
          {
              return LOG_MAIL;
          } 
          else if (name.equalsIgnoreCase("daemon")) 
          {
              return LOG_DAEMON;
          }
          else if (name.equalsIgnoreCase("auth")) 
          {
              return LOG_AUTH;
          }
          else if (name.equalsIgnoreCase("syslog")) 
          {
              return LOG_SYSLOG;
          }
          else if (name.equalsIgnoreCase("lpr")) 
          {
              return LOG_LPR;
          }
          else if (name.equalsIgnoreCase("news"))
          {
              return LOG_NEWS;
          }
          else if (name.equalsIgnoreCase("uucp")) 
          {
              return LOG_UUCP;
          }
          else if (name.equalsIgnoreCase("cron")) 
          {
              return LOG_CRON;
          }
          else if (name.equalsIgnoreCase("authpriv")) 
          {
              return LOG_AUTHPRIV;
          }
          else if (name.equalsIgnoreCase("ftp")) 
          {
              return LOG_FTP;
          }
          else if (name.equalsIgnoreCase("local0")) 
          {
              return LOG_LOCAL0;
          }
          else if (name.equalsIgnoreCase("local1")) 
          {
              return LOG_LOCAL1;
          } 
          else if (name.equalsIgnoreCase("local2")) 
          {
              return LOG_LOCAL2;
          } 
          else if (name.equalsIgnoreCase("local3")) 
          {
              return LOG_LOCAL3;
          } 
          else if (name.equalsIgnoreCase("local4")) 
          {
              return LOG_LOCAL4;
          } 
          else if (name.equalsIgnoreCase("local5")) 
          {
              return LOG_LOCAL5;
          } 
          else if (name.equalsIgnoreCase("local6")) 
          {
              return LOG_LOCAL6;
          } 
          else if (name.equalsIgnoreCase("local7")) 
          {
              return LOG_LOCAL7;
          }
          return LOG_USER;
      }
  
      public void setFacility(String name)
      {
          facility = getFacilityValue(name);
      }
  
      public void setFacility(int value)
      {
          facility = value;
      }
  
      public int getFacility()
      {
          return facility;
      }
  }
  
  
  1.1                  avalon/logging/logkit/plugins/syslog/src/java/org/apache/avalon/logging/logkit/factory/syslog/SyslogTargetFactory.java
  
  Index: SyslogTargetFactory.java
  ===================================================================
  /* 
   * Copyright 2004 Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.avalon.logging.logkit.factory.syslog;
  
  import java.io.IOException;
  import java.net.InetAddress;
  import java.net.UnknownHostException;
  
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  
  import org.apache.avalon.logging.logkit.LogTargetFactory;
  import org.apache.avalon.logging.logkit.LogTargetException;
  import org.apache.avalon.logging.logkit.FormatterFactory;
  
  import org.apache.log.LogTarget;
  import org.apache.log.format.Formatter;
  import org.apache.log.output.net.SocketOutputTarget;
  
  /**
   * SyslogTargetFactory
   *
   * This factory creates LogTargets with a wrapped SyslogTarget around it:
   *
   * <pre>
   *
   * &lt;target id="syslog"
   *       artifact="avalon-logging/avalon-logkit-syslog#1.0-SNAPSHOT"&gt;
   *   &lt;address hostname="hostname" port="514" facility="USER"/&gt;
   *   &lt;format type="extended"&gt;
   *       %7.7{priority} %23.23{time:yyyy-MM-dd HH:mm:ss:SSS}   [%25.25{category}] : %{message}\n%{throwable}
   *   &lt;/format&gt;
   * &lt;/syslog&gt;
   * </pre>
   *
   * <p>
   *  This factory creates a SyslogTarget object which will sends syslog style messages to the
   *  specified address. The name of the target is specified by the hostname attribute
   *  of the &lt;address&gt; element and the port by the port attribute.The &lt;address&gt; element
   *  wraps the format to output the log.
   * </p>
   *
   * @author <a href="mailto:sshort@postx.com"> Steve Short </a>
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2004/02/20 22:44:11 $
   */
  public class SyslogTargetFactory implements LogTargetFactory
  {
      //--------------------------------------------------------------
      // static
      //--------------------------------------------------------------
  
      private static final Resources REZ =
        ResourceManager.getPackageResources( SyslogTargetFactory .class );
  
      //--------------------------------------------------------------
      // immutable state
      //--------------------------------------------------------------
  
      private final FormatterFactory m_formatter;
  
      //--------------------------------------------------------------
      // constructor
      //--------------------------------------------------------------
  
      public SyslogTargetFactory( FormatterFactory formatter )
      {
          m_formatter = formatter;
      }
  
      //--------------------------------------------------------------
      // LogTargetFactory
      //--------------------------------------------------------------
  
      /**
       * Creates a log target based on Configuration
       *
       * @param conf Configuration requied for creating the log target
       * @throws ConfigurationException if something goes wrong while reading from
       *          configuration
       */
      public LogTarget createTarget( final Configuration conf )
          throws LogTargetException
      {
          Configuration formatConfig = conf.getChild( "format" );
          final Formatter formatter = 
            m_formatter.createFormatter( formatConfig );
  
          final Configuration configChild = 
            conf.getChild( "address", false );
          if( null == configChild )
          {
              final String error = 
                REZ.getString( "syslog.error.missing-address" );
              throw new LogTargetException( error );
          }
  
          final InetAddress address = getAddress( configChild );
          String name = getFacilityName( configChild );
          int facility = SyslogTarget.getFacilityValue( name );
          int port = getPort( configChild );
  
          try
          {
              return new SyslogTarget( address, port, formatter, facility );
          }
          catch( Throwable e )
          {
              final String error = 
                REZ.getString( "syslog.error.internal" );
              throw new LogTargetException( error, e );
          }
      }
  
      private InetAddress getAddress( Configuration config ) throws LogTargetException
      {
          try
          {
              return InetAddress.getByName( 
                  config.getAttribute( "hostname" ) );
          }
          catch( UnknownHostException uhex )
          {
              final String error = 
                REZ.getString( "syslog.error.unknown-host" );
              throw new LogTargetException( error, uhex );
          }
          catch( ConfigurationException e )
          {
              final String error = 
                REZ.getString( "syslog.error.missing-host" );
              throw new LogTargetException( error, e );
          }
      }
  
      private String getFacilityName( Configuration config ) throws LogTargetException
      {
          try
          {
              return config.getAttribute( "facility" );
          }
          catch( ConfigurationException e )
          {
              final String error = 
                REZ.getString( "syslog.error.missing-facility" );
              throw new LogTargetException( error, e );
          }
      }
  
      private int getPort( Configuration config ) throws LogTargetException
      {
          try
          {
              return config.getAttributeAsInteger( "port" );
          }
          catch( ConfigurationException e )
          {
              final String error = 
                REZ.getString( "syslog.error.missing-port" );
              throw new LogTargetException( error, e );
          }
      }
  
  }
  
  
  
  1.1                  avalon/logging/logkit/plugins/syslog/src/java/org/apache/avalon/logging/logkit/factory/syslog/package.html
  
  Index: package.html
  ===================================================================
  <body>
  <p>The <code>socket</code> package contains a plugin socket log target.</p>
  </body>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org