JavaEnterprise JavaDynamically Configure Log4j Properties in a Clustered Environment

Dynamically Configure Log4j Properties in a Clustered Environment content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Log4j and the Apache Commons Logging API provide features for recording Java application logs. But suppose the applications are deployed in a clustered environment (a logical grouping of servers) for high availability and you want to create application logs by application server.

In this advanced Java tip, Suneel Gupta explains how to configure the log4j-related properties of a Java application deployed in a clustered environment. This technique allows log files to be generated dynamically on the server side.

The scenario could goes something like this:

  1. You have deployed application X in a clustered environment with two servers (s1 and s2).
  2. You want the application logs (x.log) to be created inside the server name directory (s1 and s2).

So for X application running in the s1 server, its log should be created inside the s1 directory. Conversely, for the same X application running in the s2 server, its log should be created inside the s2 directory. The s1 and s2 directories can be in the same machine or in different machines.

Below is a sample file for this scenario. It will be common inside the X application.

log4j.rootLogger=DEBUG, A3
########## Appender A3
# Set the maximum log file size (use KB, MB or GB)
# Set the number of log files (0 means no backup files at all)
# Append to the end of the file or overwrites the file at start.
log4j.appender.A3.layout.ConversionPattern=%d [%c{1}] %p - %m%n

If you look closer at the log4j.appender.A3.File= C:x.log line, you’ll notice a problem with this file. It is dictating that when applications run on servers, their log files will generate at the C: directory. In our scenario, however, X application is running on two servers (s1 and s2), which are on either the same or different machines.

Now, how can you generate the log file dynamically inside the C:s1x.log and C:s2x.log? If you don’t use a directory structure, then X application running on server s1 and s2 will overwrite the log on the C:x.log file. In this case, you will not able to determine which server created which line of the log because it’s the same file.

Enter dynamic log4j configuration. The overwrite problem can be resolved by dynamically configuring the log when application X loads In WebSphere Application Server, there is an API ServerName.getDisplayName() method that you can use to get the name of the application server. The same type of API should be available in other Java applications and Web servers. Read the server name using the server API inside the application, and set the log file path dynamically for the application.

For example, in the following line of code, the RollingFileAppender object will give the properties of the configuration, which has been loaded in the application.

RollingFileAppender r =(RollingFileAppender) Logger.getRootLogger().getAppender("A3");

You can then read the filename and its already configured properties, such as log file path and log file size, from the file. If the log path configured is C:x.log, you can update this file path so that the log file will be generated at the desired path by using the API call below.

String filename = "C:" + ServerName.getDisplayName() + "x.log"

Below is a code snippet to load or dynamically configure log path.

public class LoadLogger {
public static Logger rLogger = Logger.getRootLogger();

public static void setLogger1(String fileName,String level,String pattern)
try {
System.out.println("setlogger1(fileName,level,pattern) in ---> Called");

if(level != null){
System.out.println("setting level");
Logger.getRootLogger().setLevel(Level.INFO );
else if(level.trim().equalsIgnoreCase("DEBUG"))
Logger.getRootLogger().setLevel(Level.DEBUG );
else if(level.trim().equalsIgnoreCase("ERROR"))
Logger.getRootLogger().setLevel(Level.ERROR );
else if(level.trim().equalsIgnoreCase("WARN"))
Logger.getRootLogger().setLevel(Level.WARN );
else if(level.trim().equalsIgnoreCase("FATAL"))
else if(level.trim().equalsIgnoreCase("ALL"))
else if(level.trim().equalsIgnoreCase("OFF"))


if(Logger.getRootLogger().getAppender("myAppender") != null)
RollingFileAppender r = new RollingFileAppender();
System.out.println(" Appender Name = " + r.getName());
System.out.println(" File = " + r.getFile());
System.out.println(" File = NULL");
System.out.println(" Layout = " + r.getLayout().getClass().getName());
PatternLayout p=new PatternLayout();
System.out.println(" ConversionPattern = " + p.getConversionPattern());
System.out.println(" Threshold = " + r.getThreshold());

PatternLayout p1=new PatternLayout();


System.out.println(" setting fileName = " + r.getFile());

System.out.println("setlogger1(fileName,level,pattern) in ---> Exited");
LoadLogger.debug("Load log4j successfully................");
catch(Exception e)
System.out.println(" exception in setlogger1() in " + e);


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories