Table of Contents
Enroll in Selenium Training

Writing Test Case with Log4j logging

Steps to follow:

  1. Create a new XML file – log4j.xml and place it under the Project root folder and Paste the following code in the log4j.xml file
<?xml version="1.0" encoding="UTF-8"?>

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

<log4j:configuration xmlns:log4j="" debug="false">

<appender name="fileAppender" class="org.apache.log4j.FileAppender">

<param name="Threshold" value="INFO" />

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

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />




<level value="INFO"/>

<appender-ref ref="fileAppender"/>



Note: After pasting the code make sure that the code is exactly same, as copying from HTML may change some symbols(“) to (?). 2) Now include logging code in to your test script

package automationFramework;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import org.apache.log4j.xml.DOMConfigurator;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Log4j {

	private static WebDriver driver;

	private static Logger Log = Logger.getLogger(Log4j.class.getName());

	public static void main(String[] args) {


		// Create a new instance of the Firefox driver

        driver = new FirefoxDriver();"New driver instantiated");

        //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);"Implicit wait applied on the driver for 10 seconds");

        //Launch the Online Store Website

        driver.get("");"Web application launched");

        // Find the element that's ID attribute is 'account'(My Account)

        driver.findElement("account")).click();"Click action performed on My Account link");

        // Find the element that's ID attribute is 'log' (Username)

        // Enter Username on the element found by above desc.

        driver.findElement("log")).sendKeys("testuser_1");"Username entered in the Username text box");

        // Find the element that's ID attribute is 'pwd' (Password)

        // Enter Password on the element found by the above desc.

        driver.findElement("pwd")).sendKeys("Test@123");"Password entered in the Password text box");

        // Now submit the form. WebDriver will find the form for us from the element

        driver.findElement("login")).click();"Click action performed on Submit button");

        // Print a Log In message to the screen

        System.out.println(" Login Successfully, now it is the time to Log Off buddy.");

         // Find the element that's ID attribute is 'account_logout' (Log Out)

        driver.findElement("account_logout"));"Click action performed on Log out link");

        // Close the driver

        driver.quit();"Browser closed");


  1. Check the output file “logfile.txt”. The output will look like below:



Add Log4j Jar
Add Log4j Jar
Previous Article
Log4j LogManager
Log4j LogManager
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Virender Singh's Photo
Virender Singh

Similar Articles