问题描述
我在Linux Machine上使用Java 1.4.2(RHEL 5.4).
我们的应用程序使用log4j进行记录.我希望某些appender会创建文件名中包含主机名的文件.主机名不应得到编码,而是使用类似于 rel="nofollow">类似的系统属性.这里(请参阅$ {log4j.minutes}).
因此,问题是系统属性中是否有主机名. 根据在这里,没有默认的hostname属性.
这意味着我需要从命令行(-D标志)的代码中添加主机名作为系统属性.
我想念什么吗?
推荐答案
否.系统属性中没有默认的主机名.此外,计算机可能具有多个NetCard,每个NetCard可能会分配多个IP数字,并且每个IP-number可能具有自己的名字,因此可能有几个候选人,"默认主机名"可能是什么.
.当您提取了自己喜欢的主机名时,然后查看如何将其保存在log4j mdc中 - 这是伪装的地图 - 因此您可以在配置中的记录字符串中参考).
).其他推荐答案
Linux环境中的另一个选项是使用SyslogAppender,该选项包括输出中的主机,并允许您像集中式日志聚合一样受益.您需要将标题属性设置为true,以启用时间戳和主机在标准Syslog公约中发送.如果这样做,那么您将获得这样的输出,其中氙气是机器的主机名:
Apr 25 14:33:17 xenon INFO Some log message
在此示例中,转换模式为%-5p %c{2} - %m%n
其他推荐答案
我认为您必须扩展Fileappender 和Override activateOptions()才能将文件名属性设置为所需的值.从某些属性中读取主机名或从inetaddress.getlocalhost().gethostname().
问题描述
I'm using java 1.4.2 on Linux machine (RHEL 5.4).
Our application uses log4j for logging. I want that some appender will create files containing hostname in file name. Hostname should not be hardocoded, but rather use system properties similar to here (see ${log4j.minutes}).
So the question is if there's hostname in system properties. According to here, there's no default hostname property.
This means I need to add hostname as system property from code of from command line (-D flag).
Do I miss something?
推荐答案
No. There is no default hostname in the system properties. Also a computer may have multiple netcards, and each netcard may have several IP-numbers allocated and each IP-number may have its own name, so there might be several candidates for what the "default hostname" might be.
When you have extracted the hostname you like, then look into how you can save it in the log4j MDC - which is a Map in disguise - so you can refer to in your logging string in your configuration).
其他推荐答案
Another option in a linux environment is to use the SyslogAppender, which includes the host in the output, and allows you benefits like centralized log aggregation. You need to set the header attribute to true to enable the timestamp and host to be sent in standard syslog convention. If you do, then you get output like this, where xenon is the hostname of the machine:
Apr 25 14:33:17 xenon INFO Some log message
In this example the conversion pattern is %-5p %c{2} - %m%n
其他推荐答案
I think you'll have to extend FileAppender and override activateOptions() to set fileName property to a desired value. Read the host name from some property or get it from InetAddress.getLocalHost().getHostName().