1 16 17 package org.apache.log4j.helpers; 18 19 20 import java.io.Writer ; 21 import java.net.DatagramSocket ; 22 import java.net.InetAddress ; 23 import java.net.DatagramPacket ; 24 import java.net.UnknownHostException ; 25 import java.net.SocketException ; 26 import java.io.IOException ; 27 28 34 public class SyslogWriter extends Writer { 35 36 final int SYSLOG_PORT = 514; 37 static String syslogHost; 38 39 private InetAddress address; 40 private DatagramSocket ds; 41 42 public 43 SyslogWriter(String syslogHost) { 44 this.syslogHost = syslogHost; 45 46 try { 47 this.address = InetAddress.getByName(syslogHost); 48 } 49 catch (UnknownHostException e) { 50 LogLog.error("Could not find " + syslogHost + 51 ". All logging will FAIL.", e); 52 } 53 54 try { 55 this.ds = new DatagramSocket (); 56 } 57 catch (SocketException e) { 58 e.printStackTrace(); 59 LogLog.error("Could not instantiate DatagramSocket to " + syslogHost + 60 ". All logging will FAIL.", e); 61 } 62 } 63 64 65 public 66 void write(char[] buf, int off, int len) throws IOException { 67 this.write(new String (buf, off, len)); 68 } 69 70 public 71 void write(String string) throws IOException { 72 byte[] bytes = string.getBytes(); 73 DatagramPacket packet = new DatagramPacket (bytes, bytes.length, 74 address, SYSLOG_PORT); 75 76 if(this.ds != null) 77 ds.send(packet); 78 79 } 80 81 public 82 void flush() {} 83 84 public 85 void close() {} 86 } 87 | Popular Tags |