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

Log 4 J

Log4j is an open source logging utility that allows logging messages to files and consoles. It has two main components - loggers and appenders. Loggers are used to log information at different levels like debug, info, error. Appenders define where the logs will be saved, such as the console or files. To configure log4j, dependencies are added to pom.xml and a log4j.xml file is created defining loggers and appenders. Then log4j can be called in code to log messages at different levels that will be output to the specified appenders.

Uploaded by

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

Log 4 J

Log4j is an open source logging utility that allows logging messages to files and consoles. It has two main components - loggers and appenders. Loggers are used to log information at different levels like debug, info, error. Appenders define where the logs will be saved, such as the console or files. To configure log4j, dependencies are added to pom.xml and a log4j.xml file is created defining loggers and appenders. Then log4j can be called in code to log messages at different levels that will be output to the specified appenders.

Uploaded by

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

Log4j

“Log4j is an Open Source. It is possible to store the flow details of our Selenium
Automation in a file. During the running of test case user wants some information to be logged
in the Console. Information could be any details depends upon the purpose. We need the
information which helps the User to understand the test steps or any failure during the test case
execution.”

Log4j has two components


✓ Loggers
✓ Appenders

Loggers

It is used for logging information. To use loggers, we need to take care of


things mentioned below: -

1. Create Object of logger class: -


Loggers class is a Java-based utility which has all the
generic methods in it to use log4j.

2. Define the log level: We can define the log levels in multiple forms and levels available
here: -

➢ Debug: - It saves the debugging information and is most useful to debug an


application.

➢ Info: - It prints informational message that highlights the progress of the


application.

➢ Error: - It error events that might still allow the application to continue running.
Appenders
“In log4j, an output destination is called an appender. It allows the
destination where the logs would get saved. It supports the following types of appenders.”

➢ ConsoleAppender: - It logs to some standard output.

➢ File Appender: - It prints logs to some file at particular destination.


Configure to Log4j
1. Add dependency of log4j in POM.xml

2. Need to create “log4j.xml” file with steps as: -


Right click on Project Name >> New >> Others >> XML >> XML File
3. Copy the below code in Log4j.xml

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

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">

<appender name="fileAppender" class="org.apache.log4j.FileAppender">

<param name="Threshold" value="INFO" />

<param name="File" value="logfile.log" />

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d %-5p [%c{1}] %m


%n" />

</layout>

</appender>

<appender name="consoleAppender"
class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="INFO" />

<param name="File" value="logfile.log" />

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d %-5p [%c{1}] %m


%n" />

</layout>
</appender>

<root>

<level value="INFO" />

<appender-ref ref="fileAppender" />


<appender-ref ref="consoleAppender" />
</root>

</log4j:configuration>
4. Now Log4j.xml is created with code.

5. Create new class as “TestTwo.java”


6. Create the Object of logger class.

7. Now call log4j.xml with the help of DOMConfiuration.


8. Now define the logs in the form of info, error as.

9. Now execute the code and print log in console and create new file name as logfile.log in
project (Refresh the project).

You might also like