SyslogWriterpublic class SyslogWriter extends Writer SyslogWriter is a wrapper around the java.net.DatagramSocket class
so that it behaves like a java.io.Writer. |
Fields Summary |
---|
final int | SYSLOG_PORT | static String | syslogHostHost 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.
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 void | close()
if (ds != null) {
ds.close();
}
| public void | flush()
| public void | write(char[] buf, int off, int len)
this.write(new String(buf, off, len));
| public void | write(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);
}
|
|