1 16 17 package org.apache.log4j.net; 18 19 import java.net.DatagramSocket ; 20 import java.net.InetAddress ; 21 import java.net.DatagramPacket ; 22 import java.net.UnknownHostException ; 23 import java.net.SocketException ; 24 25 import org.apache.log4j.helpers.OptionConverter; 26 import org.apache.log4j.AppenderSkeleton; 27 import org.apache.log4j.spi.LoggingEvent; 28 import org.apache.log4j.Category; 29 import org.apache.log4j.Priority; 30 import org.apache.log4j.Layout; 31 32 import org.apache.log4j.helpers.SingleLineTracerPrintWriter; 33 import org.apache.log4j.helpers.LogLog; 34 import org.apache.log4j.helpers.QuietWriter; 35 36 37 73 public class DatagramStringAppender extends AppenderSkeleton { 74 75 79 public static final String DATAGRAM_HOST_OPTION = "DatagramHost"; 80 81 85 public static final String DATAGRAM_PORT_OPTION = "DatagramPort"; 86 87 91 public static final String DATAGRAM_ENCODING_OPTION = "DatagramEncoding"; 92 93 97 public static final String DEFAULT_HOST = "localhost"; 98 99 103 public static final int DEFAULT_PORT = 8200; 104 105 111 public static final String DEFAULT_ENCODING = null; 112 113 String host = DEFAULT_HOST; 114 int port = DEFAULT_PORT; 115 String encoding = DEFAULT_ENCODING; 116 117 SingleLineTracerPrintWriter stp; 118 QuietWriter qw; 119 120 public 121 DatagramStringAppender() { 122 this.setDestination(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_ENCODING); 123 } 124 125 public 126 DatagramStringAppender(Layout layout) { 127 this.setLayout(layout); 128 this.setDestination(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_ENCODING); 129 } 130 131 public 132 DatagramStringAppender(Layout layout, String host, int port) { 133 this.setLayout(layout); 134 this.setDestination(host, port, DEFAULT_ENCODING); 135 } 136 137 public 138 DatagramStringAppender(Layout layout, String host, int port, String encoding) { 139 this.setLayout(layout); 140 this.setDestination(host, port, encoding); 141 } 142 143 146 public 147 void close() { 148 closed = true; 149 qw = null; 152 stp = null; 153 } 154 155 public 156 void append(LoggingEvent event) { 157 if(!isAsSevereAsThreshold(event.priority)) 158 return; 159 160 if(qw == null) { 162 errorHandler.error( 163 "No host is set for DatagramStringAppender named \"" 164 + this.name + "\"."); 165 return; 166 } 167 168 String buffer = layout.format(event); 169 qw.write(buffer); 170 171 if(event.throwable != null) 172 event.throwable.printStackTrace(stp); 173 else if (event.throwableInformation != null) { 174 qw.write(event.throwableInformation); 179 } 180 } 181 182 187 public 188 void activateOptions() { 189 this.setDestination(this.host, this.port, this.encoding); 190 } 191 192 196 public 197 String [] getOptionStrings() { 198 return OptionConverter.concatanateArrays(super.getOptionStrings(), 199 new String [] { 200 DATAGRAM_HOST_OPTION, 201 DATAGRAM_PORT_OPTION, 202 DATAGRAM_ENCODING_OPTION}); 203 } 204 205 210 public 211 boolean requiresLayout() { 212 return true; 213 } 214 215 242 public 243 void setOption(String option, String value) { 244 if(value == null) return; 245 246 super.setOption(option, value); 247 248 if(option.equals(DATAGRAM_HOST_OPTION)) 249 { 250 this.host = value; 251 } 252 else if(option.equals(DATAGRAM_PORT_OPTION)) 253 { 254 this.port = OptionConverter.toInt(value, DEFAULT_PORT); 255 } 256 else if(option.equals(DATAGRAM_ENCODING_OPTION)) 257 { 258 this.encoding = value; 259 } 260 } 261 262 public 263 void setDestination(String host, int port, String encoding) { 264 if (host==null) { 265 LogLog.error("setDestination: host is null"); 266 host = DEFAULT_HOST; 267 } 268 269 this.host = host; 270 this.port = port; 271 this.encoding = encoding; 272 273 this.qw = new QuietWriter( 274 new DatagramStringWriter(host, port, encoding), 275 errorHandler); 276 this.stp = new SingleLineTracerPrintWriter(qw); 277 } 278 279 public 280 void setLayout(Layout layout) { 281 this.layout = layout; 282 } 283 } 284 | Popular Tags |