top of page

Log4j2 in Selenium Automation Testing

Logging is an important aspect of Testing Automation . During the running of test case, user wants some information to be logged in the console as well as in the file. Information could be any detail depends upon the purpose. There are multiple reasons why we may need to capture logs during the execution of testing activities. Log4j2 helps us to achieve the above objectives in Selenium Webdriver. When logging is wisely used, it can prove to be an essential tool.

Log4j is a Java based logging API that offers many log related functionalities like configuring different log levels, separate them by class or package, appending logs to different outputs, etc. Log4j 2 is the brand new version of log4j and they are a really common logging framework in many JAVA projects. For sure if the suffix is “2” which means it brings new features and improvements such as asynchronous logging. Also, log4j 1.x is deprecated and no longer maintained that’s why it is better to migrate log4j2.

We need to follow 3 steps before enabling Logger into Selenium Test Cases.

3 Steps for setting up Log4j2

Step1 : Adding Dependencies in pom.xml file

Log4j-api and Log4j-core dependencies need to be added on pom.xml

We can get the latest version from MVN Repository@Log4j

​<!-- -->






<!-- -->






Log4j2 dependencies at pom.xml

Once you update Maven project , we can see corresponding .jar files in Maven Dependencies folder.

Maven Dependencies

Step 2 : Adding Log4j2 properties file

Once we added dependencies in pom.xml then we need to add log4j properties file which will be used by Log4j API during the execution of testcase to log the activities . API Log4J 2 looks for a properties file with the name in the classpath in default. But we can keep the file in the resource folder as a good practices .

Like any other Java properties file, a file are a set of key value pairs with options to configure the various components of Log4J 2, such as loggers, appenders, and layouts. A basic file starts with a name, optional properties to be used in other parts of the file, and appender declarations. Next, we need to configure both the appenders to write log messages to the console and a file. The configuration code for the appenders is as below

​#Declare loggers

status = error

name= PropertiesConfig

appenders=console, file

rootLogger.level= info

rootLogger.appenderRefs = ref_stdout, ref_logfile

rootLogger.appenderRef.ref_stdout.ref = STDOUT

rootLogger.appenderRef.ref_logfile.ref= LOGFILE

# Direct log messages to Console

appender.console.type = Console = STDOUT

appender.console.layout.type = PatternLayout

appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

# Direct log messages to a log file

appender.file.type = File = LOGFILE



appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n in resource folder

we can configure the root logger to log debug instead of info then it will log lower level messages to the console (stdout) and file. After set up this file in resource folder then we need to create Logger Manager class in utility folder to use in test cases.

Step 3: Creating Logger Class

In the utility folder we need to create Logger class as mentioned below. This is simple template of Logger class which is a Java-based utility that has got all the generic methods already implemented to use log4j. Using Logger manager we can create level of messages based on our need. Primarily there are five kinds of log levels and below all are supported in Log4j2.

  1. All — This level of logging will log everything ( it turns all the logs on )

  2. DEBUG — print the debugging information and is helpful in development stage

  3. INFO — print informational message that highlights the progress of the application

  4. WARN — print information regarding faulty and unexpected system behavior.

  5. ERROR — print error message that might allow system to continue

  6. FATAL — print system critical information which are causing the application to crash

  7. OFF — No logging

​package dsutilities;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class LoggerLoad {

private static Logger logger = LogManager.getLogger();

public static void info(String message) {;


public static void warn(String message) {



public static void error(String message) {



public static void fatal(String message) {



public static void debug(String message) {




Logger Class in Utility Folder

Final Step: Configuring the Log4j2 into Selenium Project

Once we completed above mentioned 3 important steps , then Logger is ready to use in the selenium Test cases. Based on logger severity we can place the loggers outputs in Test cases.

When you run the Test cases , then Logger will generate the output as defined in test cases. We can see the Log in mentioned format at Console during run time also same log will be stored in file .

Logs at Console and File

Configuring Logger with Selenium project offers the following advantages −

  • Enables us to understand the test case run.

  • Log output can be saved that can be analyzed later.

  • Helps in debugging, in case of test automation failures.

  • Can also be used for auditing purposes to look at the application’s health.

Log4j2 is a logging framework written in Java that provides an easy way for logging in Selenium. In a nutshell, the framework gives out information about everything that goes on during the testcase execution. Log4j also provides insight into anything that may have gone wrong during testcase execution or automation.

Happy TESTING !!! With Logger :)

5,500 views0 comments

Recent Posts

See All


Noté 0 étoile sur 5.
Pas encore de note

Ajouter une note
bottom of page