---Advertisement---

Log4j in Selenium: How to Download & Use Log4j Properties File Best 2025

By Manisha

Updated On:

---Advertisement---
Log4j in Selenium

Introduction

Log4j is a powerful, open-source logging framework developed by Apache for Java applications. It allows structured logging, which is essential for debugging and monitoring Selenium test executions.

Why Use Log4j in Selenium?

✅ Stores Selenium execution logs in a file or database
✅ Helps in debugging test failures and automation scripts
✅ Supports multiple logging levels (DEBUG, INFO, ERROR, etc.)
✅ Works for small and large-scale projects
✅ Reduces dependency on System.out.println() for logging


1️⃣ Loggers (Responsible for Logging Information)

Loggers define what kind of information needs to be logged.

Log Levels in Log4j:

Log LevelDescription
ALLLogs everything
DEBUGUsed for debugging during test execution
INFOGeneral information about test execution
WARNWarnings about potential issues
ERRORErrors that allow tests to continue
FATALCritical errors that stop test execution
OFFDisables logging

2️⃣ Appenders (Where Logs Are Stored)

Log4j in Selenium: Appenders define where the logs should be saved.

Appender TypeDescription
ConsoleAppenderPrints logs to the console
FileAppenderSaves logs to a file
RollingFileAppenderCreates a new log file when the old one reaches a specific size

3️⃣ Layouts (Log Format)

Log4j in Selenium: Layouts define how logs appear. Common types:

  • PatternLayout (Formats logs using a pattern)
  • HTMLLayout (Stores logs in an HTML file)
  • XMLLayout (Stores logs in an XML format)

Step 1: Add Log4j JAR to Your Selenium Project

📌 Download Log4j JAR from:

For Maven Users

Add the following dependency to your pom.xml:

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

Step 2: Create the Log4j Configuration File

1️⃣ Right-click on srcNew → File
2️⃣ Name it log4j.properties
3️⃣ Copy and paste the following configuration:

properties# Root logger
log4j.rootLogger=DEBUG, file, console

# Console Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

# File Appender (Selenium logs)
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=Selenium.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

# Rolling File Appender (Manual logs)
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=Manual.log
log4j.appender.rollingFile.MaxFileSize=5MB
log4j.appender.rollingFile.MaxBackupIndex=3
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

Step 3: Implement Log4j in Selenium WebDriver Code

Create a new Java class for logging:

javaimport org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Log4jExample {
// Create logger instance
static Logger log = Logger.getLogger(Log4jExample.class);

public static void main(String[] args) {
// Configure Log4j
PropertyConfigurator.configure("log4j.properties");

// Start logging
log.info("Launching Chrome browser");

// Set WebDriver property
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();

log.info("Navigating to website");
driver.get("https://www.example.com");

log.debug("Website title: " + driver.getTitle());

log.warn("This is a warning message");
log.error("This is an error message");
log.fatal("This is a fatal error message");

// Close browser
driver.quit();
log.info("Browser closed");
}
}

🔹 LogExpert is a Windows tool for analyzing log files in real-time.
🔹 It helps in filtering, searching, and bookmarking logs.

Step 1: Download LogExpert

Step 2: Open LogExpert

1️⃣ Run LogExpert.exe
2️⃣ Click on File → Open
3️⃣ Select Selenium.log or Manual.log

Step 3: Analyze Logs in LogExpert

Follow Tail Mode: Automatically updates logs in real time
Search & Filter: Find specific errors or keywords
Bookmark & Comment: Mark important log entries
Highlight Specific Errors: Color-code error messages


IssueSolution
log4j:WARN No appenders could be foundEnsure log4j.properties is in the src folder
Logs not getting savedCheck file path in log4j.properties
Logs not updating in real-timeUse LogExpert’s Follow Tail Mode
Error: java.lang.ClassNotFoundException: org.apache.log4j.LoggerAdd Log4j JAR file or Maven dependency

Log4j is an essential tool for Selenium automation testing, providing detailed logs for better debugging and monitoring.

✔ Use Log4j to log Selenium test executions efficiently
✔ Store logs in different files for system and manual logs
✔ Integrate LogExpert for real-time log analysis
✔ Use different log levels (INFO, DEBUG, ERROR, FATAL)

Start using Log4j in Selenium today to improve test debugging and monitoring!

👉Log4j Selenium Download
👉Selenium Webdriver

Leave a Comment

Index