The host name in the file created by log4j

I am using java 1.4.2 on a Linux machine (RHEL 5.4).

In our application, log4j is used for logging. I want some appender to create files containing the host name in the file name. The hostname should not be hardcoded, but rather use system properties similar here (see $ {Log4j.minutes}).

So, the question is whether there is a host name in the system properties. According to here , there is no default hostname property.

This means that I need to add the hostname as a system property from the code from the command line (-D flag).

Did I miss something?

+2
java logging log4j
source share
3 answers

Not. There is no default host name in the system properties. In addition, a computer can have several network cards, and each network card can have several dedicated IP numbers, and each IP number can have its own name, so there can be several candidates for what might be the "default host name" .

When you retrieved the host name that you like, see how you can save it in log4j MDC, which is a hidden map, so you can refer to your logging line in your configuration).

+2
source share

Another option in the linux environment is to use SyslogAppender, which includes an output host, and gives you benefits like a centralized log aggregation. You must set the header attribute to true to enable the timestamp and host in the standard syslog convention. If you do this, you will get this output, where xenon is the host name of the machine:

Apr 25 14:33:17 xenon INFO Some log message

In this example, the conversion pattern %-5p %c{2} - %m%n

+1
source share

I think you need to extend FileAppender and override activateOptions () to set the fileName property to the desired value. Read the hostname from a property or get it from InetAddress.getLocalHost (). GetHostName ().

0
source share

All Articles