set Log4j file name at runtime in clustered environment

In a clustered environment if you use the same file name in all the clusters .you will get only one node logs and other nods logs goes missing.

for resolving the above issue change the logfile name from cluster to my code i added the node name before the file name so that each cluster will have its own logs so that you wont miss any logs in cluster environment

in the blow class i extended DailyRollingFileAppender class and overriden the serFile Method where the file name changes for each cluster.

 and also you need to change your change the appender from "org.apache.log4j.DailyRollingFileAppender"      to CustomFileAppender

import org.apache.log4j.DailyRollingFileAppender;

public class CustomFileAppender extends DailyRollingFileAppender {

       public void setFile(String file) {
               String hostname = "";
               try {
                       InetAddress addr = InetAddress.getLocalHost();
                       hostname = addr.getHostName();
               } catch (Exception e) {

               if (file.contains("//")) {
                       //System.out.println("File Name Before Changing : "+file);
                       file = file.replace("//", "//" + hostname + "-");
                       //System.out.println(" Chnaged File Name " + file);


No comments:

Post a Comment