Let me take a stab at explaining logging for Selenium with Apache Log4j today! So, without further ado, let’s dive in!!
Why do we need logging in the first place?
As an automation tester, we deal with troubleshooting, debugging and so on. Logging makes this entire process so much easier! It gives us these magical “eyes” that let us see a number of things such as,
- What the test case is actually doing
- How the application has responded to our code
- Exceptions, issues, failures, errors, etc. can be traced down to its root when logged properly with customized error messages
- Serve as a proof of successful execution at a later point of time as these can be easily saved to a database or to external files with a timestamp
- All these logs can be generated without human intervention
Now that we understood how vital it is to insert log statements into our test cases, let us decipher what this Log4j is all about.
What is Log4j
- Popular Java-based logging package developed in 1996
- Distributed under the Apache Software License and hence it is open source
- Has various levels of logging – allows us to control the volume of logged output
- Everything can be controlled by editing a simple configuration file – no need to bother with the application binary
Log4j log levels
Below are the built-in log levels that come as part of Log4j.
- OFF – turns logging off
- FATAL – severe errors leading to application termination
- ERROR – unexpected situations and runtime errors
- WARN – warning messages
- INFO – gives information about some interesting runtime events
- DEBUG – most commonly used log level, provides detailed information on the system flow
- TRACE – information with highest level of detail
There are three main components,
There can be more than one logger in an application identified by a unique name. Each logger can be configured to a specific level – debug, info, error, etc.
It is required that we create an instance of Logger class and specify a log level.
Once a Logger instance is created, we must know where to view the generated logs. That’s where Appenders come into the picture. These specify the destination or output to which messages are to be logged, for example, a file, standard output or another computer, etc. Some of the available Appenders are, FileAppender, RollingFileAppender, ConsoleAppender, SocketAppender, and so on. It is possible to log the same information to more than one output, i.e. one Logger can have multiple Appenders.
Now that we know how to generate logs and save them to a particular destination, we might be in need to render them in different formats. Each Appender has to be mapped to a particular layout. Some of the commonly used layouts are,
- PatternLayout – one-line-at-a-time log files that use a pattern string
- HTMLLayout – renders logs in HTML format
- XMLLayout – produces XML format
These three components can be configured in a configuration file which can be in XML, JSON, YAML or properties file format. In this post, we will see how to define all the components and log messages using the properties file format.
Create a Java project in Eclipse IDE, ‘Selenium’ in my case.
Step 1: Download Log4j
The first and foremost step is to download the Apache Log4j JAR from here. Clicking on the zip file format of the mirrors will navigate to a new page with the actual mirror site details from where the JAR can be downloaded to your local machine.
Extract the contents of the downloaded folder to a particular location.
Step 2: Configure Java build path
Add Log4j JAR as an external library to the build path of the project.
Let us navigate to the path where the downloaded JAR is extracted in your local machine and add it as an external JAR.
We have seen this procedure numerous times before and hence I am not re-iterating it (Refer to Step 3 of this article).
Step 3: Create necessary files
We will be creating 3 files.
- Right click on ‘src’ folder -> New -> Other -> General -> File -> Next
- Provide the ‘File name’ as “Log4j.properties” and click ‘Finish’
The result will look something like this,
Ignore the three extra packages you see for now as I have created them for my coding purposes.
- Right click on ‘src’ folder -> New -> Other -> General -> Folder -> Next
- Provide ‘Folder name’ as ‘resources’
- Right click on ‘resources’ folder and create two files
- Name these files as ‘system.log’ and ‘test.log’
- system.log – will have all the logs generated by the system
- test.log – will contain all the logs generated as a result of the manual commands provided by the user
The environment is now all set and ready for us to do some experimentation with Log4j and generate logs. As always, the JAR is placed in the GitHub repo. Let’s see that in detail in our upcoming post.<< 10j. Advanced WebDriver – Reading data from excel using POI 10l. Advanced WebDriver – Using Log4j Part 2 >>