
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
Log4j Components
1️⃣ Loggers (Responsible for Logging Information)
Loggers define what kind of information needs to be logged.
Log Levels in Log4j:
Log Level | Description |
---|---|
ALL | Logs everything |
DEBUG | Used for debugging during test execution |
INFO | General information about test execution |
WARN | Warnings about potential issues |
ERROR | Errors that allow tests to continue |
FATAL | Critical errors that stop test execution |
OFF | Disables logging |
2️⃣ Appenders (Where Logs Are Stored)
Log4j in Selenium: Appenders define where the logs should be saved.
Appender Type | Description |
---|---|
ConsoleAppender | Prints logs to the console |
FileAppender | Saves logs to a file |
RollingFileAppender | Creates 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)
How to Configure Log4j in Selenium WebDriver
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 src
→ New → 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");
}
}
How to Analyze Logs Using LogExpert Tool?
🔹 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
Common Issues & Solutions in Log4j for Selenium
Issue | Solution |
---|---|
log4j:WARN No appenders could be found | Ensure log4j.properties is in the src folder |
Logs not getting saved | Check file path in log4j.properties |
Logs not updating in real-time | Use LogExpert’s Follow Tail Mode |
Error: java.lang.ClassNotFoundException: org.apache.log4j.Logger | Add Log4j JAR file or Maven dependency |
Conclusion
Log4j is an essential tool for Selenium automation testing, providing detailed logs for better debugging and monitoring.
Key Takeaways:
✔ 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!