Integrate Log4j 2 into Selenium - pragmatictesters/selenium-webdriver-examples GitHub Wiki
Logging is crucial for debugging and understanding the test execution flow. This guide walks you through integrating Log4j 2 into your Selenium project.
Add the following dependencies to your Maven project:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.24.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.24.3</version>
</dependency>โ Replace the version with the latest one from Maven Central.
Create a file named log4j2.xml inside src/main/resources/.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="File" fileName="logs/test.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>- Logs are shown in both console and
logs/test.logfile. - You can change the
leveltodebug,info,warn, orerror. - Customize the pattern to suit your formatting needs.
Here's a practical example using Selenium WebDriver and TestNG:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
public class Log4JTest {
private static final Logger logger = LogManager.getLogger(Log4JTest.class);
WebDriver driver;
@BeforeMethod
public void setUp() {
logger.info("Setting up WebDriver");
driver = new ChromeDriver();
}
@Test
public void testGoogleSearch() {
logger.info("Navigating to Google");
driver.get("https://www.google.com");
logger.debug("Finding the search input box");
boolean isDisplayed = driver.findElement(By.name("q")).isDisplayed();
if (isDisplayed) {
logger.info("Search input is visible");
} else {
logger.warn("Search input is NOT visible");
}
try {
Assert.assertTrue(isDisplayed, "Search box should be visible");
} catch (AssertionError e) {
logger.error("Assertion failed: " + e.getMessage());
throw e;
}
logger.info("Test completed");
}
@AfterMethod
public void tearDown() {
logger.info("Closing browser");
driver.quit();
}
}| Log Level | Purpose | Selenium Example |
|---|---|---|
DEBUG |
Detailed technical steps | Element locators, waits |
INFO |
Major events | Test start/end, navigation |
WARN |
Unexpected behavior | Optional UI missing |
ERROR |
Failures | Assertion failure, exception |
FATAL |
System crash | Rare in tests, e.g., driver init fail |
After running your tests:
- Console Logs should show messages as per your configuration.
-
Log File: Go to the
logs/folder and opentest.log.
Use this for cleaner, no-frills output:
<PatternLayout pattern="[%p] %m%n" /><RollingFile name="RollingFile" fileName="logs/test.log"
filePattern="logs/test-%d{yyyy-MM-dd-HH-mm}.log.gz">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>In Jenkins or GitHub Actions:
- Archive the
logs/test.logfile for review. - Adjust log levels dynamically based on test phase (e.g., use
DEBUGduring troubleshooting).
- โ Easy to set up and extend
- ๐ ๏ธ Advanced features like async logging, rolling files
- ๐ Improves visibility into test failures and flow
- ๐ Suitable for CI/CD and scalable projects