--- /home/gunter/work/log4j/src/java/org/apache/log4j/helpers/SyslogWriter.java (revision 410485)
+++ /home/gunter/work/log4j/src/java/org/apache/log4j/helpers/SyslogWriter.java (working copy)
@@ -37,7 +37,7 @@
*/
public class SyslogWriter extends Writer {
final int SYSLOG_PORT = 514;
- String syslogHost;
+ private int port = SYSLOG_PORT;
private InetAddress address;
private DatagramSocket ds;
@@ -45,7 +45,16 @@
StringBuffer buf = new StringBuffer();
public SyslogWriter(String syslogHost) {
- this.syslogHost = syslogHost;
+ int colon = syslogHost.indexOf(':');
+ if (colon != -1) {
+ try {
+ port = parsePort(syslogHost.substring(colon + 1));
+ } catch (IllegalArgumentException e) {
+ logger.error("Illegal port specification in " + syslogHost +
+ ". Logging will go to syslog port 514.");
+ }
+ syslogHost = syslogHost.substring(0, colon);
+ }
try {
this.address = InetAddress.getByName(syslogHost);
@@ -64,6 +73,13 @@
}
}
+ private static int parsePort(String s) {
+ int no = Integer.parseInt(s);
+ if (no <= 0 || no > 0xffff)
+ throw new IllegalArgumentException();
+ return no;
+ }
+
public void write(char[] charArray, int offset, int len) throws IOException {
buf.append(charArray, offset, len);
}
@@ -77,7 +93,7 @@
logger.debug("Writing out [{}]", buf);
byte[] bytes = buf.toString().getBytes();
DatagramPacket packet =
- new DatagramPacket(bytes, bytes.length, address, SYSLOG_PORT);
+ new DatagramPacket(bytes, bytes.length, address, port);
if (this.ds != null) {
ds.send(packet);
--- /home/gunter/work/log4j/src/java/org/apache/log4j/net/SyslogAppender.java (revision 410485)
+++ /home/gunter/work/log4j/src/java/org/apache/log4j/net/SyslogAppender.java (working copy)
@@ -286,7 +286,8 @@
/**
* The SyslogHost option is the name of the the syslog host where log
- * output should go.
+ * output should go. Format: host[:port]. If no port number is specified,
+ * output goes to syslog standard port 514.
*
* WARNING If the SyslogHost is not set, then this appender will fail.
*/