Title: Log4j with selenium tutorial: How to Setup log4j logging in selenium automation framework
1Tutorial
Debugging Selenium Scripts with Logs
2Introduction
- Here, we have included advanced ideas that would
benefit from optimizing the Testing Automation
framework and produces more clarity to the users.
- We would consider the logging feature and it's
possible, debugging capabilities etc. - Benefits of Logging in Selenium Scripts
3Log4j
Log4j A Java-based Logging Utility
- Let's Discuss the technical aspects of logging.
- Log4j was a decision of collaborative endeavors
of people at Secure Electronic Marketplace for
Europe to produce a utility. - Log4j is an open source tool and licensed under
IBM Public License. - It would support us generating logs and hence
the log4j came into consideration in the year
1996. - There are three main elements that form the
implementation of log4j
41. Loggers
Logger Class It is a java based utility/API that
has grown all the generic methods already done so
that users can enable to use Log4j.
- Log Levels Log levels are commonly identified as
to printing methods. These are utilized for
printing log messages. - 5 Types of Log Levels
- error()
- warn()
- info()
- debug()
- log()
- We have to only call any of the above printing
methods over the logger instance.
52. Appenders
Now where we can get to view the logs? Answer
is "Appenders Appenders are generally applied
to define the data source/medium where the logs
should be created.
63. Layouts
Layouts are a utility that enables the user to
choose the wanted set up in which the logs would
be performed. Appenders and Layout have a tight
coupling between them. Make a note that the
user is leveraged to determine various appenders,
each map with a different layout.
7Installation
- Here we learnt about the basics of log4j and its
parts, now let's implement these phenomena for
Debugging Selenium Scripts with Logs - Logging using log4j can be performed and
configured in particularly two ways - Using Script via Programmatically
- Configuration files via Manually
- Above both suggested configuration ways have
merit as well as demerits. - Here we will configure log4j manually using
Configuration files as its comfort and
simplicity.
8Continue
To create a log4j.xml file. You can use below
code in configuration file
9Continue
- The output of the above code is
- 01-07-2014 125632 INFO GmailLogin Sample log
message - Here,
- First is Execution Date
- Second is Time in which the test step was
performed - Third is one of the log level
- Fourth is name of the test class
- Fifth is Log message
- Put the log4j.XML file into the projects root
folder/base directory now.
10Continue
Now parse the log4j.xml file
11Continue
Now we have to generate a test class
GmailLogin.java under the project. Then import
the logger class to be able to perform the log
statements. Syntax
import org.apache.log4j.Logger
Now instantiate the object of the Logger
class Syntax //object initialization for log
static Logger log Logger.getLogger(Demo.class.g
etName())
This logger will be used over the complete test
class to create the log statements.
12Conclusion
Here we used log4j use to perform logging. We
explained the essential parts that form log4j
from a usability view. With the Appenders and
layouts, you can leverage to determine the wanted
logging form/model and the data
reference/position.
References Debugging Selenium Scripts with Logs
(Log4j Tutorial) Debugging Selenium Scripts with
Logs
13Contact Us
Web www.nexsoftsys.com Email info_at_nexsoftsys.com
Call 1-646-971-0799