Logging Framework – Log4j

Reading Time: 2 minutes

Welcome folks!
In most of the client calls, we come across the following questions :
– “Send me the entire logs of the file”
– “I want the logs with timestamp”
– “I want the log file of the last week” etcetera.
To help us with all the above questions, Apache guys have provided us with a logging framework – Log4j

What is Log4j?

Log4j is a reliable, fast and flexible logging framework (API) written in Java, which is distributed under the Apache software license.

For every automation test step, there are logs for example ‘button clicked successfully’, ‘page is loaded’ etcetera.

Therefore, logs help find all the steps where a Test case gets passed and where it gets failed.
Including it is a best automation practice.

Importing Log4j

Visit the following link :
https://logging.apache.org/log4j/2.0/download.html and download the jars.
Then, Import them in your project just like another jar file.

Amongst all the jars available,log4j-core and log4j-api are the most important jars.

log4j-api, as its name says is an API. It just contains interfaces that you can use to interface your code with the log4j framework. Log4j-core is the implementation of this interface, so it contains actual code. It implements every interface in the API.


Let us suppose the class where you want to use loggers is “Demo.java”, so the syntax goes like

private static logger log = LogManager.getLogger(Demo.class.getName());

Here, log is an object which will be used to access the methods of Logger class.

We need to initialize log object with LogManager.

getLogger accepts the class arguments i.e. the entire name of the class is passed as an argument.

Commonly used methods and their usage:

log.debug (“any debug string message”);
– When each Selenium action is performed like click, SendKeys, getText()
log.error (“any error string message”);
-Use this when elements are not displayed in the page or if any validations fail
log.info(“any info string message”);
-When an operation is completed ex: After loading page, or after any successful validations
It’s just counterpart to log. Error()

Configuration file in Log4j

A configuration file is the heart of Log4j. This includes two main tags.

Appenders and Loggers

Further a do, Appenders answers two questions i.e Where to log? How to log? and loggers answer one question i.e.What to log?

If you give the <console> tag inside Appenders, then output will be printed in console. Similary, If you give the <file> tag inside Appenders, then output will be printed in file.

About Loggers, it tells inside <Root> tag what level of logging is being achieved. Example, here, error are being logged by the code.

Sample code :

As mentioned, instead of console we can use a file to print our logs.

Basically, it is an utility which can be used with any testing framework like Selenium, Appium , Rest Assured API or any other java based testing framework for logging the errors or information.

References :


Written by 

Nearly 3 years of experience in automation testing, I call myself an automation enthusiast. I can create, execute automated test scripts using framework guidelines and best practices to ensure wider and efficient end-to-end automation coverage. Simultaneous experience in Defect tracking and bug reporting through JIRA