You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ha...@apache.org on 2001/12/22 12:36:50 UTC
cvs commit: jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/logger/factory SocketTargetFactory.java DatagramTargetFactory.java
hammant 01/12/22 03:36:50
Modified: src/java/org/apache/avalon/excalibur/logger/factory
DatagramTargetFactory.java
Added: src/java/org/apache/avalon/excalibur/logger/factory
SocketTargetFactory.java
Log:
Submission from Rajendra Ghorpade (RGhorpade@onebridge.de)
Revision Changes Path
1.4 +54 -33 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/logger/factory/DatagramTargetFactory.java
Index: DatagramTargetFactory.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/logger/factory/DatagramTargetFactory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DatagramTargetFactory.java 2001/12/19 23:37:13 1.3
+++ DatagramTargetFactory.java 2001/12/22 11:36:50 1.4
@@ -3,58 +3,66 @@
*
* 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.
+ * the LICENSE 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 java.io.OutputStream;
+import org.apache.avalon.excalibur.logger.factory.AbstractTargetFactory;
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;
+import org.apache.log.output.net.DatagramLogEventTarget;
-
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
/**
* DatagramTargetFactory
*
- * This factory creates LogTargets with a wrapped
- * DatagramOutputTarget around it:
+ * This factory creates LogTargets with a wrapped DatagramOutputTarget around it:
*
* <pre>
+ *
* <datagram-target id="target-id">
* <address hostname="hostname" port="4455" />
* <format type="extended">
- * %7.7{priority} %23.23{time:yyyy-MM-dd HH:mm:ss:SSS}[%25.25{category}] : %{message}\n%{throwable}
+ * %7.7{priority} %23.23{time:yyyy-MM-dd HH:mm:ss:SSS} [%25.25{category}] : %{message}\n%{throwable}
* </format>
* </datagram-target>
* </pre>
+ *
+ * <p>
+ * This factory creates a DatagramOutputTarget object which will
+ * sends datagrams to the specified address. The name of the target is specified by the hostname attribute
+ * of the <address> element and the port by the port attribute.The <address> element
+ * wraps the format to output the log.
+ * </p>
+ *
*
- * <p>This factory creates a DatagramOutputTarget object which will
- * sends datagrams to the specified address. The name of the target is
- * specified by the hostname attribute of the <address> element and
- * the port by the port attribute. The <address> element wraps the
- * format to output the log.</p>
+ * @author <a href="mailto:rghorpade@onebridge.de"> Rajendra Ghorpade </a>
+ * @version
*/
public class DatagramTargetFactory
extends AbstractTargetFactory
{
-
+ /** Default format */
+ private static final String FORMAT =
+ "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}";
+
+ /**
+ * Create a LogTarget based on a Configuration
+ */
public LogTarget createTarget( final Configuration conf )
throws ConfigurationException
{
- final Configuration formatting = conf.getChild( "format", false );
- final Formatter formatter = getFormatter( formatting );
+ InetAddress address;
final Configuration configChild = conf.getChild( "address", false );
if ( null == configChild )
@@ -62,37 +70,50 @@
throw new ConfigurationException( "target address not specified in the config" );
}
- InetAddress address;
try
{
address = InetAddress.getByName( configChild.getAttribute( "hostname" ) );
}
- catch ( final UnknownHostException uhe )
+ catch ( UnknownHostException uhex )
{
- throw new ConfigurationException( "Host specified in datagram target adress is unknown!", uhe );
+ throw new ConfigurationException( "Host specified in datagram target adress is unknown!", uhex );
}
+
+ int port = configChild.getAttributeAsInteger( "port" );
+
+ final Formatter formatter = getFormatter( conf.getChild( "format", false ) );
- final int port = configChild.getAttributeAsInteger( "port" );
try
{
- return new DatagramOutputTarget( address, port, formatter );
+ return new DatagramLogEventTarget( address, port, formatter );
}
- catch ( final IOException ioe )
+ catch ( IOException ioex )
{
- throw new ConfigurationException( "Failed to create target!", ioe );
+ throw new ConfigurationException( "Failed to create target!", ioex );
}
}
+ /**
+ * Returns the Formatter
+ *
+ * @param conf Configuration for the formatter
+ */
protected Formatter getFormatter( final Configuration conf )
{
- Formatter formatter = null;
+ final String type = conf.getAttribute( "type", "pattern" );
+ final String format = conf.getValue( FORMAT );
- if ( null != conf )
+ if( "extended".equals( type ) )
{
- final FormatterFactory formatterFactory = new FormatterFactory();
- formatter = formatterFactory.createFormatter( conf );
+ return new ExtendedPatternFormatter( format );
}
-
- return formatter;
+ else if( "raw".equals( type ) )
+ {
+ return new RawFormatter();
+ }
+
+ /** default formatter */
+ return new PatternFormatter( format );
}
}
+
1.1 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/logger/factory/SocketTargetFactory.java
Index: SocketTargetFactory.java
===================================================================
package org.apache.avalon.excalibur.logger.factory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.log.LogTarget;
import org.apache.log.output.net.SocketOutputTarget;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
/**
* SocketTargetFactory
*
* This factory creates LogTargets with a wrapped SocketOutputTarget around it:
*
* <pre>
* <socket-target id="target-id">
* <address hostname="hostname" port="4455" />
* </socket-target>
* </pre>
*
* <p>
*
* This factory creates a SocketOutputTarget object which will
* TCP/IP socket to communicate with the server. The name of the target is specified by the
* hostname attribute of the <address> element and the port by the port attribute.
* In the config file above the formatting for the log messages is not embedded as it should
* be specified on the server side
*
* </p>
*
*
* @author <a href="mailto:rghorpade@onebridge.de"> Rajendra Ghorpade </a>
* @version
*/
public class SocketTargetFactory extends AbstractTargetFactory
{
/**
* Creates a log target based on Configuration
*
*@param conf Configuration requied for creating the log target
*@exception ConfigurationException if something goes wrong while reading from
* configuration
*/
public LogTarget createTarget( final Configuration conf )
throws ConfigurationException
{
final InetAddress address;
final Configuration configChild = conf.getChild( "address", false );
if ( null == configChild )
{
throw new ConfigurationException( "target address not specified in the config" );
}
try
{
address = InetAddress.getByName( configChild.getAttribute( "hostname" ) );
}
catch ( UnknownHostException uhex )
{
throw new ConfigurationException( "Host specified in socket target adress is unknown!", uhex );
}
final int port = configChild.getAttributeAsInteger( "port" );
try
{
return new SocketOutputTarget( address, port );
}
catch ( final IOException ioex )
{
throw new ConfigurationException( "Failed to create target!", ioex.fillInStackTrace() );
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>