
In this tutorial, we will introduce the logging capability to our framework. Logs are the files which are generated during run time. Log4j API is used to generate the logs.
To add logs in our framework, we should:
- create a source folder src/main/syslog at the project root level
- create a file log4j.properties inside the syslog which should contain the logging configurations for the log which we want to generate
- specify a file name with location where the log should be generated inside log4j.properties file
You can also create the log4j.properties inside properties folder along with config.properties and or.properties.
Your Project Explorer should look like:

log4j.properties file
Paste the following code to log4j.properties file:
#application logs
log4j.logger.devpinoyLogger=DEBUG,app
log4j.appender.app=org.apache.log4j.RollingFileAppender
log4j.appender.app.maxFileSize=5000KB
log4j.appender.app.maxBackupIndex=1
log4j.appender.app.layout=org.apache.log4j.PatternLayout
log4j.appender.app.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
#file name with location where the log should be generated
log4j.appender.app.File=/home/dhawal/eclipse-workspace/PageObjectNoPageFactory/src/test/resources/com/appliedselenium/logs/application.log
#this will always create a new file
log4j.appender.app.Append=false
Discussing the log4j.properties is way beyond the scope of this tutorial. You can reuse the same configuration for multiple projects with slight modifications or visit the ORACLE website to learn more.
Next, we will update the BasePage.java class to initialize the logger. Once initialzed, you may put the logger at every validation or critical step.
Add log to BasePage.java
In the BasePage.java class, declare and define the Logger instance variable as follows:
public static Logger log = Logger.getLogger("devpinoyLogger");
Now we can use the log instance variable to generate the logs. For instance, let’s use this log to capture the following events:
- when config.properties and or.properties are loaded
- name of the invoked browser
- name of the application url
- inside click() method to log the element name which is clicked
- inside enter() method to log the value and element name
We will use log.debug() to capture the log at that step. Further update the BasePage.java class as following to include the above events.
package com.appliedselenium.pages;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.appliedselenium.pages.commons.NavBar;
public class BasePage{
// Declare the WebDriver
public static WebDriver driver;
//declare variable for config.properties
public static Properties config = new Properties();
//declare variable for or.properties
public static Properties or = new Properties();
//File input stream to read from properties file
public static FileInputStream fis;
//Declare NavBar object variable
public static NavBar navBar;
//Create instance for Logger object
public static Logger log = Logger.getLogger("devpinoyLogger");
// constructor of base class
public BasePage() {
if (driver == null) {
try {
//Take reference to the config.properties file
fis = new FileInputStream(System.getProperty("user.dir")
+ "/src/test/resources/com/appliedselenium/properties/config.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//load the config.properties file
config.load(fis);
//application log
log.debug("config.properties file loaded");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Take reference to the or.properties file
try {
fis = new FileInputStream(
System.getProperty("user.dir") + "/src/test/resources/com/appliedselenium/properties/or.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//load the or.properties file
try {
or.load(fis);
log.debug("or.properties file loaded");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Set the path for chromedriver
String filePath = System.getProperty("user.dir");
//verify if the browser is chrome or FF
if (config.getProperty("browser").equals("chrome")) {
System.setProperty("webdriver.chrome.driver",
filePath + "/src/test/resources/com/appliedselenium/drivers/chromedriver");
driver = new ChromeDriver();
}else if (config.getProperty("browser").equals("firefox")) {
//launch firefox
}
log.debug(config.getProperty("browser")+ " browser invoked");
// go to application
driver.get(config.getProperty("appurl"));
log.debug("Navigated to "+config.getProperty("appurl"));
driver.manage().window().maximize();
//Invoke the NavBar object with driver reference
navBar = new NavBar(driver);
}
}
public void click(String element) {
if(element.contains("XP")) {
driver.findElement(By.xpath(or.getProperty(element))).click();
}
if(element.contains("ID")) {
driver.findElement(By.id(or.getProperty(element))).click();
}
if(element.contains("NAME")) {
driver.findElement(By.name(or.getProperty(element))).click();
}
log.debug("Clicked on "+ element + " web element");
}
public void enter(String element, String value) {
if(element.contains("XP")) {
driver.findElement(By.xpath(or.getProperty(element))).sendKeys(value);
}
if(element.contains("ID")) {
driver.findElement(By.id(or.getProperty(element))).sendKeys(value);
}
if(element.contains("NAME")) {
driver.findElement(By.name(or.getProperty(element))).sendKeys(value);
}
log.debug("Entered value: " + value + " in web element " +element);
}
}
Code highlighted in bold will create the application.log file inside logs folder. Refresh the project if it is not already displayed after execution.

In the next tutorial we will refactor the LoginPageTest.java class and create separate test scripts. We will also learn to implement Listeners and extent report.
Please comment below if you have any query till this point, as we will soon be completing with this POM tutorial series.
Hi,
I copy pasted verbatim most of the contents of the “log4j.properties” file shown in this blog, except that the following line:
log4j.appender.app.File=/home/dhawal/eclipse-workspace/PageObjectNoPageFactory/src/test/resources/com/appliedselenium/logs/application.log
is replaced with the following line:
log4j.appender.app.File=C:\Users\my_windows_id\Documents\selenium\workspaces\workspace_AppliedSelenium\Part7_POM_by_AppliedSelenium\src\test\resources\com\appliedselenium\logs\application.log
Still, the log file “application.log” file is not generated.
Instead, the following messages are displayed:
log4j:WARN No appenders could be found for logger (devpinoyLogger).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I also noticed that the folder name where the “log4j.properties” file is saved is mentioned as “syslogs”, where as in the screenshots shown, the name is shown as “syslog”, although changing the name to either did not make a difference.
Can you please advise?