Make Selenium Easy

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


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.

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:-

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.


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:-


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.

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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 3 min
%d bloggers like this: