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
Log4j has three principal components
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.
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
#Root logger #Application Logs In the above Example, we have configured log4j to log in two different files named as Selenium.log and Manual.log.
file and dest1 are the two identifiers. “File” is used to give file name in which logs will be saved “maxFileSize” is used to configure the maximum size of the log file. When file reaches this size, a new file will be created with the same name and the old file name will be add as an Index to it. “maxBackupIndex” is used to configure maximum number of files to be backup. “layout” is used to set the format of the log file. “Append” is used to set append function. If it is set to false, than every time a new file will be created rather than old file will be used for logging
How log4j is used within script?
In code, we have used “log” as a reference variable referencing getLogger method of Logger Class Use “log” referencing variable and debug method to log the information we want.
What is a LogExpert tool?
LogExpert tool is a tool for Windows developed to tail the logs It is free and open source log viewer. It is a log analysis tool with multiple features like searching, filtering, bookmarking and highlighting the logs In this tool logs, files get automatically updated when opened In this tool, we can open multiple log file in different tabs We can also put comments on bookmarks, and there is the shortcut key to navigate in between different bookmarks. We can also see complete bookmark list and navigate from there Shortcuts of the tool are given in help file so that they can be referred to the tool.
Steps to use Log4j with Selenium
Step 1) In Eclipse create a new project with name log4j_demo
Step 2) Right click on src -> Build Path -> Configure Build Path
Step 2) Click on Libraries and Add Log4J Library . You can download it from https://logging.apache.org/log4j/1.2/download.html
Step 3) Create a new file. This file will include all the log4j configuration
Right click on src -> New -> Other -> General -> File Give the file name as “log4j.properties” Click on Finish
Create two more files and give them names such as Selenium.logs and Manual.logs. These files will contain all the logs created by system and manually logged statements
Step 4) In log4j.properties copy the entire configuration.
Step 5) Create main class:
Right click on default package -> New -> Class Give the class name and click on finish
Step 6) Copy the following code in to the main class In the above code, we visit https://healthunify.com/bmicalculator/and verify BMI calculator. The weight entered is 87KG and the height is 5 Feet 10 inches. The script checks output in SE, US and UK units. Using Logger.getLogger(“devpinoyLogger”) we create system level logs Using log.debug method we store data into Manual.log Step 7) Run the script. Open the location of Manual and Selenium logs to check logging data.
How LogExpert tool can be used to analyze logs
Download the tool from https://github.com/zarunbal/LogExpert . Go to LogExpert download folder
Open LogExpert.exe
Click on File -> Open and Browse to the path where Manual.log and Selenium.log files are stored. Select the file
Select the “Follow tail” option
Selecting follow tail option enables tailing of logs which means LogExpert automatically updates the log file when script is in execution phase. If we use any other editor like notepad then we have to close and reopen the file again and again to update the logs. But with ExpertTool in Follow Tail Mode this is not required. Following images shows the layout of the logs
Using LogExpert tool, one can debug logs created by the selenium webdriver as in this tool once can Selecting follow tail option enables tailing of logs which means LogExpert automatically updates the log file when script is in execution phase. If we use any other editor like notepad then we have to close and reopen the file again and again to update the logs. But with ExpertTool in Follow Tail Mode this is not required. Following images shows the layout of the logs
search for any text and regular expression, create bookmark and comment them and also can navigate between bookmarks which is not possible in any other tool, Filter the logs and search for text ranges and also can apply another filter to the previous filtered logs, Highlight different line based on some certain words.
This tool also helps to partition the data into different columns.