views:

74

answers:

3

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.

+1  A: 

Have you tried sniffing the local network traffic to see if the log messages are actually sent, if they seem well-formed, etc? You could use nast or something like it.

gustafc
Did not know it was possible when the traffic is loopback. But I haven't tried nast, so here goes ...
Synesso
`nast` was just the first thing I found on Google, I just assumed it can sniff loopback. WireShark can, anyway: http://wiki.wireshark.org/CaptureSetup/Loopback
gustafc
Question edited with update.
Synesso
+1  A: 

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

Thorbjørn Ravn Andersen
The firewall is not active.jem@dsupport2:~/projects$ sudo ufw statusStatus: inactive
Synesso
well, ufw is not enabled, but wireshark says destination unreachable ... what other way could the port be blocked?
Synesso
Use the GUI version instead. I do not know which technology is used under the hood.
Thorbjørn Ravn Andersen
A: 

Syslogd is probaby not listening on an IP socket but a unix domain socket. The standard socket is /dev/log. You will need to use a library such as JUDS to connect to this socket. This will give an OutputStream that you can write the log record to.

Geoff Reedy