Table of Contents

Continuing the series of tutorials on Log4j, lets try to understand what Log4j LogManager is and how we can use it.

LogManager, as the name suggests, is the Manager of all logger objects. This is the static class that you refer to for creating Logger objects. LogManager also keeps a list of all the loggers being created by the application. If I were to summarize, LogManager does the following work

- Create instances of Logger objects.

- Store references of all the created logger objects.

- Allow reuse of the same logger object in different parts of the code.

Let's look at this example code. This shows how we can create different logger instances out of a LogManager.

package Log4jSample;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class SampleEntry {

	public static void main(String[] args) {

                //This is how we create a logger object
		Logger logger1 = LogManager.getLogger("Logger1");
		Logger logger2 = LogManager.getLogger("Logger2");
		Logger logger3 = LogManager.getLogger("Logger3");
		BasicConfigurator.configure();
		logger1.info("This is logger 1");
		logger2.info("This is logger 2");
		logger3.info("This is logger 3");		
	}
}

We can also retrieve all the logger objects inside LogManager at a particular instance by using the getCurrentLoggers() method. Here is the code sample

package Log4jSample;

import java.util.Enumeration;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class SampleEntry {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Logger logger1 = LogManager.getLogger("Logger1");
		Logger logger2 = LogManager.getLogger("Logger2");
		Logger logger3 = LogManager.getLogger("Logger3");
		BasicConfigurator.configure();
		logger1.info("This is logger 1");
		logger2.info("This is logger 2");
		logger3.info("This is logger 3");	

		Enumeration loggers = LogManager.getCurrentLoggers();
		while(loggers.hasMoreElements())
		{
			logger3.info("Logger name is " + loggers.nextElement().getName());			
		}
	}
}

One very important property of LogManager it lets us retrieve an existing logger object by name. Also, if we try to create a logger object with the same name as an existing logger object, it will pass on the reference of the existing logger object instead of creating one. This can be shown in the code below

package Log4jSample;

import java.util.Enumeration;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class SampleEntry {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Logger logger1 = LogManager.getLogger("Logger1");
		Logger logger2 = LogManager.getLogger("Logger2");
		Logger logger3 = LogManager.getLogger("Logger3");
		BasicConfigurator.configure();
		logger1.info("This is logger 1");
		logger2.info("This is logger 2");
		logger3.info("This is logger 3");	

		Logger logger1_2 = LogManager.getLogger("Logger1");
		Logger logger1_3 = LogManager.getLogger("Logger1");
		//You will see that LogManager doesnt create a new instance of logger
		//Object with name Logger1, instead passes on the reference to the 
		//existing Logger1 object. We can confirm this with following lines
		logger1_2.info("The name of this logger is " + logger1_2.getName());

        if(logger1_3.equals(logger1))
        {
        	logger1_3.info("Both objects are same");
        }
        else
        {
        	logger1_3.info("Logger1 and logger1_2 are different objects");
        }

	}
}

Out put of this code is

1 [main] INFO Logger1 - This is logger 1
2 [main] INFO Logger2 - This is logger 2
2 [main] INFO Logger3 - This is logger 3
2 [main] INFO Logger1 - The name of this logger is Logger1
2 [main] INFO Logger1 - Both objects are same

As you can see from the last two lines of output both logger1_2 and logger1_3 are pointing to the same logger object which was created at the start of the program. This way we can reuse the same logger across different parts of the test code.

Root Logger Object

All logger in an application follows a hierarchy where root logger falls at the top of the hierarchy. Any logger that you create can be traced back to the root logger.

Loggers follow the hierarchy which is similar to the class hierarchy. Any logger with full name as a.b.c will have b as the father and a as the ancestor. To show the root logger in hierarchy, this is how a logger object hierarchy would be Root.a.b where Root is the ancestor of b. To prove that all loggers are children of root logger see the below code.

package Log4jSample;

import java.util.Enumeration;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class SampleEntry {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Logger logger1 = LogManager.getLogger("Logger1");
		Logger logger2 = LogManager.getLogger("Logger2");
		Logger logger3 = LogManager.getLogger("Logger3");
		BasicConfigurator.configure();
		logger1.info("This is logger 1");
		logger2.info("This is logger 2");
		logger3.info("This is logger 3");	

		Logger rootLogger  = LogManager.getRootLogger();
		Logger rootOf1 = logger1.getRootLogger();

		if(rootOf1.equals(rootLogger))
		{
			rootOf1.info("Both rootLogger and rootOf1 are same objects");	
			rootOf1.info("The Name of the root logger is " + rootLogger.getName());
		}
		else{
		     rootOf1.info("Both rootLogger and rootOf1 are different objects");
		}		
	}
}

I hope this tutorial helps you understand the usage and working of LogManager. Keep tuned for more tutorials on Log4j.

Thanks Virender

Test Case with Log4j
Test Case with Log4j
Previous Article
Log4j Appenders
Log4j Appenders
Next Article
Virender Singh
I am Virender Singh, I have around 14 years of experience in the Technology domain.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback