如何让 Syslogging 在 JVM 上工作?
我想从 Java 进行系统日志记录。有一个 log4j 附加程序,但它似乎不起作用(无论如何对我来说......尽管谷歌结果显示许多其他人的问题仍未解决)。
我正在尝试调试附加程序,因此我根据 RFC3164
它运行,但系统日志中没有显示任何日志记录。
// 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
我也尝试过使用/bin/nc,但它也不起作用。
echo "<14>May 26 15:23:33 Hello world" > nc -u localhost 514
不过,Ubuntu 命令 /usr/bin/logger 确实可以工作。
logger -p user.info hello world
# logs: May 26 15:25:10 dsupport2 jem: hello world
我可能做错了什么?
编辑
nc 和scala 生成以下数据包:
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
看来我无法让 /usr/bin/logger (有效的)进行远程通话。我假设您应该将本地 syslogd 设置为中继。
编辑
使用 nc,wireshark 显示消息格式正常,但端口无法访问。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ubuntu 中的网络防火墙需要明确告知允许流量到达给定端口,这包括 Syslog。
The network firewall in Ubuntu needs to be explicitly told to allow traffic to a given port, this includes Syslog.
您是否尝试过嗅探本地网络流量以查看日志消息是否实际发送,它们是否看起来格式良好等?您可以使用 nast 或类似的东西。
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.
Syslogd 可能不是在 IP 套接字上侦听,而是在 UNIX 域套接字上侦听。标准套接字是
/dev/log
。您将需要使用诸如 JUDS 之类的库来连接到此套接字。这将提供一个 OutputStream,您可以将日志记录写入其中。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.