Input your search keywords and press Enter.

Configure log4j logging in Selenium Automation Framework

debug true log4j logging

When you search How to set up log4j logging configuration in Java– it will give the appropriate result, instead of searching with How to set up log4j in Selenium. Basically, log4j logging is a part of Java and it elaborates on a broader extent with respect to Java development.

Now you got the today’s agenda. We are going to discuss logging through log4j in quite a simple and easier manner so that you can implement in your framework without any trouble. If you are not much friendly with the programming, then I would simply recommend you follow the steps discussed in this tutorial and your logging will be ready in your Selenium project.

Recommended: How to perform read write operations with Properties file in Selenium?

What is the importance of logging in Automation framework?

Logging basically helps to capture the message at runtime. The message could be anything like a success message, failure message, error message or any fatal message. These log messages are stored at the central location where we can fetch it easily.

How to configure log4j logging in Eclipse for Selenium Automation framework?

At first, you need to add the log4j jar file to your Selenium project. You can do this either by directly adding jar file or by creating dependency in your pom.xml file.

Click here to download log4j jar file.

Copy and paste below dependency in your pom.xml file if you don’t wish to download the jar file.

How to configure log4j logging in Selenium project?

Step 1: Creation of log4j.xml

Once you have added the jar file or created the dependency then you need to define a log4j.xml where all the details about appender and file are available.

Just copy or paste below file and name it as log4j.xml

Creating ExecutionLog.log

Check this line in log4j.xml

This line tells the compiler about the central file where all the logs will be appended. So make sure to add “ExecutionLog.log” file in your project’s root directory. If you wish to change the name of the file name then you must edit this line in log4j.xml

Step 2: Create log configurator class

Now create a class, let’s say Log.class. Here you do DOM configuration of the log4j.xml. Method inside this class will return getLogger(class name). Copy and paste below code in your Log.class file.

Step 3: Calling of the method inside Log.class file

Now you simply implement below code wherever you want to implement logging. Here it creates references to the Log.class and passes the current class name as the reference.

Some of the methods inside logger class are as follows:

We user below methods wherever required.

  • info(object message);
  • error(object message);
  • debug(object message);
  • fatal(object message);
  • warn(object message);
  • trace(object message);

Sample log4j logging program

Console Output:

Log4j logging console output

ExecutionLog.log:

Execution log for log4j logging

 

Tip:

When you set debug as true in log4j.xml then you get elaborated logs in printed in the console.

The console log will be like this-

debug true log4j logging

You can join our Facebook group for latest updates on Automation testing.

Join Inviul fb group

Avinash Mishra is the author of Inviul blog. He is a software engineer and blogger by choice. He loves to write blogs, and apart from blogging, he is interested in documentary film making, listening to music, traveling around the world and philanthropic activities.

Leave a Reply

Your email address will not be published. Required fields are marked *