1 17 18 package org.apache.avalon.logging.logkit.factory.syslog; 19 20 import java.net.InetAddress ; 21 import java.net.UnknownHostException ; 22 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 26 import org.apache.avalon.logging.logkit.LogTargetFactory; 27 import org.apache.avalon.logging.logkit.LogTargetException; 28 import org.apache.avalon.logging.logkit.FormatterFactory; 29 30 import org.apache.avalon.util.i18n.ResourceManager; 31 import org.apache.avalon.util.i18n.Resources; 32 33 import org.apache.log.LogTarget; 34 import org.apache.log.format.Formatter; 35 36 62 public class SyslogTargetFactory implements LogTargetFactory 63 { 64 68 private static final Resources REZ = 69 ResourceManager.getPackageResources( SyslogTargetFactory .class ); 70 71 75 private final FormatterFactory m_formatter; 76 77 81 public SyslogTargetFactory( FormatterFactory formatter ) 82 { 83 m_formatter = formatter; 84 } 85 86 90 97 public LogTarget createTarget( final Configuration conf ) 98 throws LogTargetException 99 { 100 Configuration formatConfig = conf.getChild( "format" ); 101 final Formatter formatter = 102 m_formatter.createFormatter( formatConfig ); 103 104 final Configuration configChild = 105 conf.getChild( "address", false ); 106 if( null == configChild ) 107 { 108 final String error = 109 REZ.getString( "syslog.error.missing-address" ); 110 throw new LogTargetException( error ); 111 } 112 113 final InetAddress address = getAddress( configChild ); 114 String name = getFacilityName( configChild ); 115 int facility = SyslogTarget.getFacilityValue( name ); 116 int port = getPort( configChild ); 117 118 try 119 { 120 return new SyslogTarget( address, port, formatter, facility ); 121 } 122 catch( Throwable e ) 123 { 124 final String error = 125 REZ.getString( "syslog.error.internal" ); 126 throw new LogTargetException( error, e ); 127 } 128 } 129 130 private InetAddress getAddress( Configuration config ) throws LogTargetException 131 { 132 try 133 { 134 return InetAddress.getByName( 135 config.getAttribute( "hostname" ) ); 136 } 137 catch( UnknownHostException uhex ) 138 { 139 final String error = 140 REZ.getString( "syslog.error.unknown-host" ); 141 throw new LogTargetException( error, uhex ); 142 } 143 catch( ConfigurationException e ) 144 { 145 final String error = 146 REZ.getString( "syslog.error.missing-host" ); 147 throw new LogTargetException( error, e ); 148 } 149 } 150 151 private String getFacilityName( Configuration config ) throws LogTargetException 152 { 153 try 154 { 155 return config.getAttribute( "facility" ); 156 } 157 catch( ConfigurationException e ) 158 { 159 final String error = 160 REZ.getString( "syslog.error.missing-facility" ); 161 throw new LogTargetException( error, e ); 162 } 163 } 164 165 private int getPort( Configuration config ) throws LogTargetException 166 { 167 try 168 { 169 return config.getAttributeAsInteger( "port" ); 170 } 171 catch( ConfigurationException e ) 172 { 173 final String error = 174 REZ.getString( "syslog.error.missing-port" ); 175 throw new LogTargetException( error, e ); 176 } 177 } 178 179 } 180 | Popular Tags |