Search code examples
javascalasyslog

How can I get Syslogging to work on the JVM?


I want to do syslogging from Java. There is a log4j appender, but it doesn't seem to work (for me anyway ... though Google results show many others with this issue still unresolved).

I'm trying to debug the appender, so I've written the following script based upon RFC3164

It runs, but no logging appears in the syslog.

// scala
import java.io._
import java.net._
val ds = new DatagramSocket()
val fullMsg = "<11>May 26 14:47:22 Hello World"
val packet = new DatagramPacket(fullMsg.getBytes("UTF-8"), fullMsg.length, 
  InetAddress.getLocalHost, 514)
ds send packet
ds.close

I also tried using /bin/nc, but it doesn't work either.

echo "<14>May 26 15:23:33 Hello world" > nc -u localhost 514

The Ubuntu command /usr/bin/logger does work, however.

logger -p user.info hello world
# logs: May 26 15:25:10 dsupport2 jem: hello world

What could I be doing wrong?

Edit

Both nc & the scala generate the following packet:

jem@dsupport2:~/projects/log4j$ grep -A 10 514 out
xxx.xxx.xxx.xxx:37920(unknown) -> xxx.xxx.xxx.xxx:514(syslog)
Version: 4   Total Lenght: 63   TTL: 64
Packet Number: 4

---[ UDP Data ]------------------------------------------------------

<14>May 26 15:26:33 Hello world 22

It seems I cannot get /usr/bin/logger (the one that works) to talk remotely. I assume you're supposed to set up the local syslogd as a relay.

Edit

Using nc, wireshark shows the message to be formatted OK, but that the port is unreachable.


Solution

  • The network firewall in Ubuntu needs to be explicitly told to allow traffic to a given port, this includes Syslog.