Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Log4j2 Tutorial 1 – Introduction To Apache Log4j2

Posted on 12/11/2024 By admin

Let’s start with a scenario.

Suppose you are developing an E-commerce application. There will be many modules but just to make it simple let’s suppose you have three modules to develop – searchProduct, calculateValue, and payment. A user will search for a product and add it to the cart. Then cart value will be calculated and the user will be navigated to the payment page. We have a simple flow here.

Data will be moved across modules. While developing you must have to add some standard print commands so that you could track the flow. For example – items are searched correctly, desired items are added to the cart, total payable amount, etc. These standard print statements help you to analyze the code flow and catch if there is any unexpected result or break in the flow. In short a kind of log, it is. These logs will be very important for development, testing, and finding the place of occurrence of a problem later.

The use of standard print statements is not a good way of logging. It will be really difficult to track these print statements in a pool of extra logs coming from other sources (Maven, Gradle, libraries, etc). You may also want to store logs in an external file or keep track of old logs then it may not be easy. The standard print statements will not help much in a production environment.

There are multiple logging libraries are available in Java. You can use Java Logging APIs or Apache Log4j2.

It does not matter if you are a developer or an automation tester you must need to use Logger in your framework and test scripts. In this post, we will learn about Apache Log4j2 Java library.

Apache Log4j is a Java-based logging framework. It has 2 versions – Log4j 1 and Log4j2. Log4j2 is an upgraded version of Log4j 1. We should always use the latest version as it provides more features and bug fixes.

To use Log4j2 in your framework, you just need to add the below libraries:-

log4j-api-.jar
log4j-core-.jar

We can configure Log4j2 with our application using a configuration file written in XML, JSON, YAML, or properties format. We can do it programmatically as well but let’s focus on configuring using configuration files as of now. Log4j has the ability to automatically configure itself during initialization. It has an order to look for the configuration file in the application. Log4j will provide a default configuration if it cannot locate a configuration file.

Log levels are a mechanism to categorise logs. Levels used for identifying the severity of an event. We can easily configure levels to specify which log details we want to see. Log4j provides below levels:-

  1. ALL – To log all events.
  2. DEBUG – A general debugging event.
  3. ERROR – An error in the application, possibly recoverable.
  4. FATAL – A severe error that will prevent the application from continuing.
  5. INFO – An event for informational purposes.
  6. TRACE – A fine-grained debug message, typically capturing the flow through the application.
  7. WARN – An event that might possibly lead to an error.
  8. OFF – No events will be logged.

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.

We can specify destinations to keep event logs. We may want to print those logs in the console or any external file. Appenders usually are only responsible for writing the event data to the target destination. We may use multiple appenders.

In some upcoming posts, we will learn Log4j2 with some examples.

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

Uncategorized

Post navigation

Previous Post: rest assured maven project setup
Next Post: learn selenium with quiz

Related Posts

image – Make Selenium Easy Uncategorized
TestNG Tutorials 36: Can a Test Method Return a Value in TestNG? – Make Selenium Easy Uncategorized
February 10, 2018 – Make Selenium Easy Uncategorized
January 2018 – Make Selenium Easy Uncategorized
Selenium Interview Question 11 – Which Class Implements Alert Interface in Selenium WebDriver? Uncategorized
November 2018 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark