Wednesday, February 18, 2009

Log4j, Websphere - Print server or host name to application log

My project runs on Websphere server and uses a cluster of two servers. Both the servers log to the same application log file. After a bad production issue, we found it necessary to log the server name to the application log file, so as to be able to track activity on individual servers. Since the code is already in production, I didn't want to change it. Log4j documentation didnt help, and I didn't wish to dwelve into MDC.

Here is a simple trick that worked for me.

Websphere has an inbuilt variable WAS_SERVER_NAME, which basically has the name of the server (e.g. Server1, Server2 etc).

1. Using the Websphere Admin Console , I created a JVM Custom property.

Key = WAS_SERVER_NAME

Value = ${WAS_SERVER_NAME}

2. I referred the above property in my log4j.properties file.

log4j.appender.X.layout.conversionPattern=%d %-5p [%C] ${WAS_SERVER_NAME} %m%n

(The text in bold is the extra word that I added)

Now log4j prints the server name in the application log file.

This idea can be used to print any extra information, like host name etc!

No comments:

Post a Comment