简体   繁体   English

SyslogServer没有收到UDP消息

[英]SyslogServer not receiving UDP messages

I'm currently using Syslog4j 0.9.46 for handling syslog UDP messages. 我当前正在使用Syslog4j 0.9.46处理syslog UDP消息。 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: 要发送在jar上执行的syslog messages命令:

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 . 我在Syslog4j 0.9.46文档中检查了以下术语: SyslogServerIFAbstractSyslogServerUDPNetSyslogServer 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. 我还在http://www.syslog4j.org/上搜索了FAQ和提供的示例,但是它们都与服务器无关。

So my question is. 所以我的问题是。 How can I get the UDP syslog server running without having to call the main method from SyslogServer ? 如何在不必从SyslogServer调用main方法的情况下使UDP syslog服务器运行? I don't use any custom handlers. 我不使用任何自定义处理程序。

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. 获取服务器实例时,Syslog4j不提供默认事件处理程序。 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. 添加此事件处理程序将在标准输出上打印传入的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM