You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2001/12/14 22:35:29 UTC

cvs commit: jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/logger/factory DatagramTargetFactory.java

donaldp     01/12/14 13:35:29

  Added:       src/java/org/apache/avalon/excalibur/logger/factory
                        DatagramTargetFactory.java
  Log:
  Add a factory for datagram log targets.
  
  Submitted By: Ghorpade, Rajendra <RG...@onebridge.de>
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/logger/factory/DatagramTargetFactory.java
  
  Index: DatagramTargetFactory.java
  ===================================================================
  /*
   * 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.avalon.excalibur.logger.factory;
  
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  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.log.LogEvent;
  import org.apache.log.LogTarget;
  import org.apache.log.format.ExtendedPatternFormatter;
  import org.apache.log.format.Formatter;
  import org.apache.log.format.PatternFormatter;
  import org.apache.log.format.RawFormatter;
  import org.apache.log.format.SyslogFormatter;
  import org.apache.log.output.net.DatagramOutputTarget;
  
  public class DatagramTargetFactory
      extends AbstractTargetFactory
  {
      private static final String FORMAT =
          "%7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): %{message}\\n%{throwable}";
  
      public LogTarget createTarget( final Configuration conf )
          throws ConfigurationException
      {
          final Formatter formatter = getFormatter( conf.getChild( "format", false ) );
  
          final Configuration configChild = conf.getChild( "address", false );
          if ( null == configChild )
          {
              throw new ConfigurationException( "target address not specified in the config" );
          }
  
          InetAddress address;
          try
          {
              address = InetAddress.getByName( configChild.getAttribute( "hostname" ) );
          }
          catch ( final UnknownHostException uhe )
          {
              throw new ConfigurationException( "Host specified in datagram target adress is unknown!", uhe );
          }
  
          final int port = configChild.getAttributeAsInteger( "port" );
          try
          {
              return new DatagramOutputTarget( address, port, formatter );
          }
          catch ( final IOException ioe )
          {
              throw new ConfigurationException( "Failed to create target!", ioe );
          }
      }
  
      protected Formatter getFormatter( final Configuration conf )
      {
          final String type = conf.getAttribute( "type", "pattern" );
          final String format = conf.getValue( FORMAT );
  
          if( "extended".equals( type ) )
          {
              return new ExtendedPatternFormatter( format );
          }
          else if( "raw".equals( type ) )
          {
              return new RawFormatter();
          }
          else if( "syslog".equals( type ) )
          {
              return new SyslogFormatter();
          }
          else
          {
              return new PatternFormatter( format );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>