Configure log4j logging in Selenium Automation Framework

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.

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

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

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
	debug="false">

	<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
		<param name="Threshold" value="INFO" />
		<param name="File" value="ExecutionLog.log" />
		 <param name="MaxFileSize" value="15000KB" />
		<layout class="org.apache.log4j.EnhancedPatternLayout">
			<param name="ConversionPattern" value="[%p] [%c{1}] [%M] %m %n %throwable{20}" />
		</layout>
	</appender>
	
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<param name="threshold" value="info" />
		<layout class="org.apache.log4j.EnhancedPatternLayout">
			<param name="ConversionPattern" value="[%p] [%c{1}] [%M] %m %n %throwable{20}" />
		</layout>
	</appender>
	
	<!-- <appender name="parallellogger" >
	    
	</appender> -->


	<root>
		<level value="INFO" />
		<appender-ref ref="console" />
		<appender-ref ref="fileAppender" />
		<!-- <appender-ref ref="parallellogger" /> -->
	</root>

</log4j:configuration>

Creating ExecutionLog.log

Check this line in log4j.xml

<param name="File" value="ExecutionLog.log" />

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.

package com.inviul.selenium.project;

import java.io.File;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log {
	
	public static Logger getLogData(String className){
		String path = new File("").getAbsolutePath();
		DOMConfigurator.configure("log4j.xml");
		return Logger.getLogger(className);
	}

}

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.

 final Logger logger = Log.getLogData(Log.class.getName());

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

package com.inviul.selenium.project;

import org.apache.log4j.Logger;
import org.testng.annotations.Test;


public class Log4jDemo {

  public static void main(String[] args)  {
	  final Logger logger = Log.getLogData(Log.class.getName());
	  
	  logger.info("This is info message");
	  logger.error("This is error message");
	  logger.debug("This is debug message");
	  logger.fatal("This is fatal ");
	  logger.warn("This is warn message");
	  logger.trace("This is trace message");
  }
}
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.

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
	debug="true">

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

Leave a Reply