javasyslog4j

SyslogServer not receiving UDP messages


I'm currently using Syslog4j 0.9.46 for handling syslog UDP messages. I use it to send them from the command line and now I have to receive them somehow. To do this I'm using the following code:

SyslogServerIF server = SyslogServer.getInstance("udp");
server.getConfig().setHost("127.0.0.1");
server.getConfig().setPort(514);
server.initialize("udp", server.getConfig());
server.run();
Thread.sleep(60*1000);

Apparently this is not enough or I'm doing something wrong. To send syslog messages command performed on the jar:

java -jar syslog4j-0.9.46.jar -h 127.0.0.1 -p 514 udp

I know they are being sent correctly, because running the following code allows me to receive them:

SyslogServer.main(new String[]{"-h", "127.0.0.1", "-p", "514", "udp"});

I checked the following terms in the Syslog4j 0.9.46 documentation: SyslogServerIF, AbstractSyslogServer and UDPNetSyslogServer. Every single one of them contained just the return value, parameters and what the method throws without a single word of description. I also searched the FAQ and the provided examples at http://www.syslog4j.org/, but none of them were about the server.

So my question is. How can I get the UDP syslog server running without having to call the main method from SyslogServer? I don't use any custom handlers.


Solution

  • After a lot of debugging, coffee and a long debate with my rubber duck finally I found the solution. Syslog4j does not provide a default event handler when getting an instance of a server. For future generations who will be forced to struggle with the same problem:

    SyslogServerIf server = SyslogServer.getInstance("udp");
    SyslogServerConfigIf config = server.getConfig();
    config.addEventHandler(new PrintStreamSyslogServerEventHandler(System.out));
    syslogServer.run();
    

    Adding this event handler will print the incoming messages on the standard output.