1 8 package org.apache.avalon.excalibur.logger.factory; 9 10 import java.io.OutputStream ; 11 import org.apache.avalon.excalibur.logger.factory.AbstractTargetFactory; 12 import org.apache.avalon.framework.configuration.Configuration; 13 import org.apache.avalon.framework.configuration.ConfigurationException; 14 import org.apache.log.LogTarget; 15 import org.apache.log.format.ExtendedPatternFormatter; 16 import org.apache.log.format.Formatter; 17 import org.apache.log.format.PatternFormatter; 18 import org.apache.log.format.RawFormatter; 19 import org.apache.log.output.net.DatagramOutputTarget; 20 21 import java.io.IOException ; 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 25 51 public class DatagramTargetFactory 52 extends AbstractTargetFactory 53 { 54 55 private static final String FORMAT = 56 "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}"; 57 58 61 public LogTarget createTarget( final Configuration conf ) 62 throws ConfigurationException 63 { 64 InetAddress address; 65 66 final Configuration configChild = conf.getChild( "address", false ); 67 if ( null == configChild ) 68 { 69 throw new ConfigurationException( "target address not specified in the config" ); 70 } 71 72 try 73 { 74 address = InetAddress.getByName( configChild.getAttribute( "hostname" ) ); 75 } 76 catch ( UnknownHostException uhex ) 77 { 78 throw new ConfigurationException( "Host specified in datagram target adress is unknown!", uhex ); 79 } 80 81 int port = configChild.getAttributeAsInteger( "port" ); 82 83 final Formatter formatter = getFormatter( conf.getChild( "format", false ) ); 84 85 try 86 { 87 return new DatagramOutputTarget( address, port, formatter ); 88 } 89 catch ( IOException ioex ) 90 { 91 throw new ConfigurationException( "Failed to create target!", ioex ); 92 } 93 } 94 95 100 protected Formatter getFormatter( final Configuration conf ) 101 { 102 final String type = conf.getAttribute( "type", "pattern" ); 103 final String format = conf.getValue( FORMAT ); 104 105 if( "extended".equals( type ) ) 106 { 107 return new ExtendedPatternFormatter( format ); 108 } 109 else if( "raw".equals( type ) ) 110 { 111 return new RawFormatter(); 112 } 113 114 115 return new PatternFormatter( format ); 116 } 117 } 118 119 | Popular Tags |