如何在Log4j写入Linux syslog的消息中更改localhost.localdomain [英] How can I change localhost.localdomain in the messages written by Log4j to the Linux syslog
问题描述
我正在使用Log4j及其SyslogAppender将消息写入Linux syslog.输出的消息如下所示:
I'm writing messages to the Linux syslog using Log4j and its SyslogAppender. The outputted messages look like this:
Oct 12 09:06:03 localhost.localdomain 3364 [Thread-5] INFO TEST MESSAGE MyApplicationLogger - This is my message
我想将localhost.localdomain替换为运行该应用程序的服务器的名称,但似乎无法弄清楚该怎么做.如果有用的话,这是我的配置文件:
I would like to replace localhost.localdomain with the name of the server running the application but can't seem to figure out how. Here's my configuration file, in case it's useful:
log4j.logger.MyApplicationLogger=INFO, SyslogAppender
log4j.appender.SyslogAppender=org.apache.log4j.net.SyslogAppender
log4j.appender.SyslogAppender.syslogHost=localhost
log4j.appender.SyslogAppender.Facility=USER
log4j.appender.SyslogAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.SyslogAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
log4j.appender.SyslogAppender.source=My Application
编辑:在服务器上配置了计算机名称,并且hostname命令返回以下内容:
Edit: the computer name is configured on the server and the hostname command returns the following:
$ hostname
server12
但是我确实注意到hosts文件的第一行是
I do notice however that the first line of the hosts file is
127.0.0.1 localhost.localdomain localhost
但是我仍然不愿意更改它.
However I'm hesitant to change this.
推荐答案
我想出了解决方法,所以如果其他人遇到相同的问题,我也最好将解决方案留在这里.
I figured out how to fix it, so I might as well leave the solution here in case other people face the same problem.
在属性文件中,输入以下内容:
In the properties file, put this:
log4j.appender.SyslogAppender.syslogHost=server12
不确定为什么可以使用,但是使用服务器名称代替localhost可以解决我的问题.
Not sure why it works, but using the server name instead of localhost worked for me.
这篇关于如何在Log4j写入Linux syslog的消息中更改localhost.localdomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!