Make Selenium Easy

Log4j2 Tutorial 1 – Introduction To Apache Log4j2

Why we need Loggers?

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.

About Log4j2

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.

Required dependencies

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

Configuration

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

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.

Appenders

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

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 1 – Introduction To Apache Log4j2

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