public SyslogWriter(String syslogHost) {
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);
}
}
Constructs a new instance of SyslogWriter. Parameters:
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.
|