Log4j2 Tutorial 2 – Creating Log4j2 Maven Project With Default Configuration File

Introduction

We have already seen why Logger is important and why it should be part of your project. Whether you are an automation tester or a developer, a Logger is an important part of your code.

In this post, we will see a very simple implementation of Apache Log4j2 in a Maven project without any configuration file setup.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Create Maven project with Log4j2 dependency

Creating a maven project should not be difficult. Create a maven project and add the latest dependency of Apache Log4j2 Core in pom.xml. I am using the below version which is the latest while writing this post.



    org.apache.logging.log4j
    log4j-core
    2.14.1

Obtain a Logger

This is the very first step we need to do. We need to create a Logger for the class for which we want to capture the logs. A logger is created to call or log messages based on different levels. Loggers also help to identify the Java types to understand the flow. Log4j2 provides an interface named Logger which is a central interface in the Log4j2 package and responsible for most of the logging operations.

The canonical way to obtain a Logger for a class is through LogManager.getLogger() in which the default logger name typically a fully qualified class name is obtained.

Use levels to log

Just adding the Logger as a depencdecny and creating a Logger will not log anything. You need to explicitly log details using levels in your code which I have discussed in this post.

Example Code

Let’s perform above two steps in below Java class:-

package appCode;

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

public class ModuleA {
	
	// Creating a logger
	private static Logger logger = LogManager.getLogger();

	// Log messages
	public static void main(String[] args) {
		
		logger.debug("It is a debug logger.");
		logger.error("It is an error logger.");
		logger.fatal("It is a fatal logger.");
		logger.info("It is a info logger.");
		logger.trace("It is a trace logger.");
		logger.warn("It is a warn logger.");

	}
}

You can observe that I have created a Logger using LogManager and explicitly using all the levels with a string message. Logging is developer’s responsibility and they need to log with proper and meaningful description.

Now run the above program as a Java program.

Output

18:07:15.984 [main] ERROR appCode.ModuleA - It is an error logger.
18:07:15.987 [main] FATAL appCode.ModuleA - It is a fatal logger.

We have used all the levels but in the console, we are seeing only two levels. Actually when we do not provide any configuration file(yet to be covered), by default Log4j uses a default configuration.

The default configuration, provided in the DefaultConfiguration class, will set up:

  • A ConsoleAppender attached to the root logger i.e. logs will be printed on the console.
  • A PatternLayout set to the pattern “%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} – %msg%n” attached to the ConsoleAppender

Note that by default Log4j assigns the root logger to Level.ERROR and those logs will be printed on the standard console.

Already we know from the previous post that Log4j follows order as below:-

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL

If we mention log level as INFO then all INFO, WARN, ERROR and FATAL events will be logged. If we mention log level as WARN then all WARN, ERROR and FATAL events will be logged. In simple terms, all the levels below the specified level including the specified level will be considered.

So since the default level is set to ERROR, both error and fatal will be logged.

Let’s understand the pattern format in which logs are printed. Since we have not passed any configuration file, it uses the default format as shown above. Let’s visualize output with the default pattern.

%d{HH:mm:ss.SSS} is execution timestamp i.e. 18:07:15.984. [%t] is thread name i.e. [main]. %-5level is level name i.e. ERROR. %logger{36} is logger name which we are creating as first step i.e. appCode.ModuleA. %msg%n is message i.e. “It is an error logger” followed by a new line character.

We will learn more about formatting in upcoming posts. As of now, logger{36} is to cap the length of the logger name to 36 characters. %-5level is to print log level with right padded to 5 spaces.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Leave a Reply

Your email address will not be published. Required fields are marked *