Alex Loddengaard wrote:
I'd like my log messages to display the hostname of the node that they were outputted on. Sure, this information can be grabbed from the log filename, but I would like each log message to also have the hostname. I don't think log4j provides support to include the hostname in a log, so I've tried programmatically inserting the hostname with the following three approaches. These are all within a log4j Appender. -Using exec to run "hostname" from the command line. This returns null. -Using InetAddress.getLocalHost().getHostName(). This returns null. -Using InetAddress.getLocalHost().getHostAddress(). This returns null.
You sure your real/virtual hosts networking is set up right? I've seen problems in hadoop there
https://issues.apache.org/jira/browse/HADOOP-3426 Have a look/apply that patch and see what happens
Each of these approaches works in an isolated test, but they all return null when in Hadoop's context. I believe I'd be able to get the hostname with a Java call to a Hadoop configuration method if I were in a Mapper or Reducer, but because I'm in a log4j Appender, I don't have access to any of Hadoop's configuration APIs. How can I get the hostname?
Log4J appenders should have access to the hostname info, But you are going to risk time and trouble if you do that in every operation; every new process runs a risk of a 30s delay even if you cache it from then on. It is usually a lot faster/easier just to push out the IP address, as that doesn't trigger a reverse DNS lookup or anything.