0% found this document useful (0 votes)
5 views

Logging in Selenium

The document provides a comprehensive guide on setting up Log4j2 logging in Selenium, including necessary Maven dependencies and steps to create a Logger instance. It details how to customize logging using Log4j2.properties and Log4j2.xml configuration files, specifying the structure and settings for console, file, and HTML log outputs. Additionally, it includes examples of log output formats for both log files and HTML files.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Logging in Selenium

The document provides a comprehensive guide on setting up Log4j2 logging in Selenium, including necessary Maven dependencies and steps to create a Logger instance. It details how to customize logging using Log4j2.properties and Log4j2.xml configuration files, specifying the structure and settings for console, file, and HTML log outputs. Additionally, it includes examples of log output formats for both log files and HTML files.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LOG4J2 LOGGING IN SELENIUM

Kus

LOG4J2 LOGGING IN
SELENIUM

Kushal Parikh
[email protected]
https://www.linkedin.com/in/kushalparikh11/
Kushal Parikh
QA Automation Engineer

Log4j2 Logging in Selenium

Log4j2 Setup with Log4j2.properties & Log4j2.xml

Step 1: Add Maven Log4j2 Dependencies

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.23.1</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>

KUSHAL PARIKH 1
Kushal Parikh
QA Automation Engineer

Step 2: Creating Logger instance in Class

To start logging messages using this basic configuration, all you need to
just obtain a Logger instance using the LogManager class:

private static Logger logger =


LogManager.getLogger(YourClass.class);

YourClass = Give name of your respective class

Then you can use the logger object with methods corresponding to the log
level you want:

logger.error("This is an error message");


logger.info("This is an info message");
logger.debug("This is an debugging message");

For log level info :


https://logging.apache.org/log4j/2.x/manual/customloglevels.html

Note: Use this in the class you want to add log like base.class,
extentManager.class etc .

KUSHAL PARIKH 2
Kushal Parikh
QA Automation Engineer

Step 3: Customizing logging by adding


Log4j2.properties or Log4j2.xml

Create a log4j2.properties or log4j2.xml file in the resources folder as


shown (recommended).
Note: You can create it anywhere you want, but src/resources is
recommended.

Folder structure:

KUSHAL PARIKH 3
Kushal Parikh
QA Automation Engineer

Using Log4j2.properties

status = warn
name=PropertiesConfig
property.filename = logs

#Set up apprenders for file & console


appenders = console, file ,htmlFile

# Set up for Console log output


appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss} [%t] %c{1} - %msg%n

# Set up for LOGFILE log output


appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/automationLogs_${date:yyyy-MM-dd_HH-mm-ss}.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss} [%t] %c{1} - %msg%n

# Set up for HTML log output


appender.htmlFile.type = File
appender.htmlFile.name = HTMLFILE
appender.htmlFile.fileName=${filename}/automationLogs_${date:yyyy-MM-dd_HH-mm-ss}.html
appender.htmlFile.layout.type=HTMLLayout
appender.htmlFile.layout.datepattern=yyyy-MM-dd'T'HH:mm:ss
appender.htmlFile.layout.timezone=GMT+0
appender.htmlFile.layout.LocationInfo=true

# Level of Logs
rootLogger.level = info
rootLogger.appenderRefs = file
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.logfile.ref = LOGFILE
rootLogger.appenderRef.htmlfile.ref = HTMLFILE

Using Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>


<Configuration status="WARN">
<Appenders>
KUSHAL PARIKH 4
Kushal Parikh
QA Automation Engineer

<Console name="Console" target="SYSTEM_OUT">


<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level - %msg%n"/>
</Console>
<File name="File" fileName="logs\\automationLogs_${date:yyyy-MM-dd_HH-mm-ss}.log"
append="true">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n</Pattern>
</PatternLayout>
</File>
<!-- Add HTML File Appender -->
<File name="htmlFile" fileName="logs\\automationLogs_${date:yyyy-MM-dd_HH-mm-
ss}.html">
<HtmlLayout title="HTML Layout Demo" datePattern="ISO8601" timezone="GMT+0"/>
</File>
</Appenders>
<Loggers>
<Logger name="com.seleniumsessions" level="info" additivity="true">
<appender-ref level="info" ref="File"/>
<!-- Add HTML File Appender to the logger -->
<appender-ref level="info" ref="htmlFile"/>
</Logger>
<Root additivity="true" level="info">
<appender-ref ref="Console"/>
</Root>
</Loggers>
</Configuration>

Logfile Log output:

KUSHAL PARIKH 5
Kushal Parikh
QA Automation Engineer

HTML Log output:

KUSHAL PARIKH 6

You might also like