How to Generate log files using Log4j in Selenium WebDriver

Published: 10 September 2017
on channel: Specialize Automation
4,073
18

Configuring log4j in selenium webdriver step by step and generating different types of logs using different login levels.

log4j tutorial selenium webdriver,
Introduction of log4j in Java,
Introduction of Log4j in selenium webdriver,
logging framework in selenium webdriver,
How to Generate Log Files in Selenium Webdriver,
How to configure Log4j in selenium,
How to set up log4j in selenium,
Setting up log4j in selenium,
Setting up log4j in java,
log4j properties files,


Introduction Log4j
Log4j is a fast, flexible and reliable logging framework (APIS) written in Java developed in early 1996. It is distributed under the Apache Software License. Log4J has been ported to the C, C++, C#, Perl, Python, Ruby and Eiffel Languages. It is a tool used for small to large scale Selenium Automation projects.

Why use Log4j?
It is an open source
With Log4j, it is possible to store the flow details of our Selenium Automation in a file or databases
Log4j is used for large as well as small projects
In Log4j, we use log statements rather than SOPL statements in the code to know the status of a project while it is executing

How log4j is configured?
To configure log4j we have to decide which appender to implement. Accordingly, parameters of appender will be set.

We will use DEBUG level and RollingFileAppender
We will do two configurations or logs,
First: root logger, that will write all system generated logs in file name i.e. Selenium.logs
Second: Will write the information generated by manual commands in code into the file name- Manual.logs
Layout will be PatternLayout

Loggers: It is responsible for logging information. To implement loggers into a project following steps need to be performed -
Create an instance for logger class: Logger class is a Java-based utility that has got all the generic methods already implemented to use log4j
Define the Log4j level: Primarily there are five kinds of log levels
All - This level of logging will log everything ( it turns all the logs on )
DEBUG – print the debugging information and is helpful in development stage
INFO – print informational message that highlights the progress of the application
WARN – print information regarding faulty and unexpected system behavior.
ERROR – print error message that might allow system to continue
FATAL – print system critical information which are causing the application to crash
OFF – No logging
Appenders: It is used to deliver LogEvents to their destination. It decides what will happen with log information. In simple words, it is used to write the logs in file. Following are few types of Appenders
ConsoleAppender logs to standard output
File appender prints logs to some file
Rolling file appender to a file with maximum size
Note: In log4j properties we can call appender with any name. There are other appenders as well but we will restrict to these few.

Layouts: It is responsible for formatting logging information in different styles.
The Logger class provides different methods to handle logging activities. It provides two static methods for obtaining a Logger Object.

Public static Logger getRootLogger()
Public static Logger getLogger(String name)

Linkedin :   / aditya-kumar-roy-b3673368