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 cluster.in 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 log4j.properties change the appender from "org.apache.log4j.DailyRollingFileAppender"      to CustomFileAppender

CustomFileAppender.java
--------------------------

import org.apache.log4j.DailyRollingFileAppender;

public class CustomFileAppender extends DailyRollingFileAppender {

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

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

}

No comments:

Post a Comment