FileDocCategorySizeDatePackage
SyslogWriter.javaAPI DocApache log4j 1.2.154182Sat Aug 25 00:09:40 BST 2007org.apache.log4j.helpers

SyslogWriter

public class SyslogWriter extends Writer
SyslogWriter is a wrapper around the java.net.DatagramSocket class so that it behaves like a java.io.Writer.
since
0.7.3

Fields Summary
final int
SYSLOG_PORT
static String
syslogHost
Host string from last constructed SyslogWriter.
private InetAddress
address
private final int
port
private DatagramSocket
ds
Constructors Summary
public SyslogWriter(String syslogHost)
Constructs a new instance of SyslogWriter.

param
syslogHost host name, may not be null. A port may be specified by following the name or IPv4 literal address with a colon and a decimal port number. To specify a port with an IPv6 address, enclose the IPv6 address in square brackets before appending the colon and decimal port number.


                                                                       
  
     
    SyslogWriter.syslogHost = syslogHost;
    if (syslogHost == null) {
        throw new NullPointerException("syslogHost");
    }
    
    String host = syslogHost;
    int urlPort = -1;
    
    //
    //  If not an unbracketed IPv6 address then
    //      parse as a URL
    //
    if (host.indexOf("[") != -1 || host.indexOf(':") == host.lastIndexOf(':")) {
        try {
            URL url = new URL("http://" + host);
            if (url.getHost() != null) {
                host = url.getHost();
                //   if host is a IPv6 literal, strip off the brackets
                if(host.startsWith("[") && host.charAt(host.length() - 1) == ']") {
                    host = host.substring(1, host.length() - 1);
                }
                urlPort = url.getPort();
            }
        } catch(MalformedURLException e) {
      		LogLog.error("Malformed URL: will attempt to interpret as InetAddress.", e);
        }
    }
    
    if (urlPort == -1) {
        urlPort = SYSLOG_PORT;
    }
    port = urlPort;

    try {      
      this.address = InetAddress.getByName(host);
    }
    catch (UnknownHostException e) {
      LogLog.error("Could not find " + host +
			 ". All logging will FAIL.", e);
    }

    try {
      this.ds = new DatagramSocket();
    }
    catch (SocketException e) {
      e.printStackTrace();
      LogLog.error("Could not instantiate DatagramSocket to " + host +
			 ". All logging will FAIL.", e);
    }
    
  
Methods Summary
public voidclose()

      if (ds != null) {
          ds.close();
      }
  
public voidflush()

public voidwrite(char[] buf, int off, int len)

    this.write(new String(buf, off, len));
  
public voidwrite(java.lang.String string)


    if(this.ds != null && this.address != null) {
        byte[] bytes = string.getBytes();
        //
        //  syslog packets must be less than 1024 bytes
        //
        int bytesLength = bytes.length;
        if (bytesLength >= 1024) {
            bytesLength = 1024;
        }
        DatagramPacket packet = new DatagramPacket(bytes, bytesLength,
                               address, port);
        ds.send(packet);
    }