#TECH

How to use Log4j with Selenium Web Driver

What is log4j ?

Log4j is an open source logging framework. With log4j – logging behavior can be controlled by editing a configuration file only without touching the application binary and can be used to store the Selenium Automation flow logs. It equips the user with detailed context for application failures.

log4j has 3 major components:

  1. 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: Logger class is a Java-based utility which has all the generic methods in it to use log4j (To use Logger class we need to Import org.apache.log4j.Logger)
    2. Define the log level: We can define the log levels in multiple forms and levels available are:
      1. All – This level of logging will log everything, it is intended to turn on all logging.
      2. DEBUG – It saves the debugging information and is most helpful to debug an application.
      3. INFO – It prints informational message that highlights the progress of the application.
      4. WARN – It designates potentially harmful situations.
      5. ERROR – It designates error events that might still allow the application to continue running.
      6. FATAL – It designates very severe error events that will presumably lead the application to crash
      7. OFF – It is intended to turn off logging.
  2. 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:-
    1. ConsoleAppender – It logs to some standard output.
    2. File appender – It prints logs to some file at a particular destination.
    3. Rolling file appender – It is used to for a log file with maximum size.
  3. Layouts – It is used to format the logging information in different style.

Start set-up of log4j in Eclipse

  1. Download and add jar file of log4j from this link
  2. Add this jar file to src folder with steps as:
    Right click on src -> Build Path -> Configure Build Path -> Libraries -> Add External JARs -> “Browse the folder in which you have saved the jar files” -> Select the jar file -> Click open -> Click OK
    Note: Jar file should get added to the list of JAR files in “Referenced Libraries”
  3. Need to create 3 new files
    a) Configuration file – Add a new file named as “log4j.properties” with steps as:
    Right click on src -> new -> other -> general -> file -> next -> File name as “log4j.properties” -> Click on Finish button
    Note: This file should be added to source folder and it will have all the configuration settings
    b) Log file for logs generated by manual commands:-

    Add a new file named as “test.logs”(this should be added in the package) to capture the information logs generated by manual commands in code with steps as:
    Right click on “package name” -> new -> other -> general -> file -> next -> File name as “test.logs” -> Click on Finish button
    Note: A new file should be added to the package, it will write the information generated by manual commands in code.
    c) Log file for system generated logs:-
    Add a new file named as “selenium.txt” to a location in any drive(location should be accessible by other apps, location should not have admin only rights like we have in C drive generally)
    Note: This file will write all system generated logs.

Copy the below code snippet in log4j.properties file
//Start copying from the next line

#Root logger option – This section will help to log the system generated logs in Selenium.txt file

log4j.rootLogger=debug,file
#Here debug is the Logger level and file is used as an identifier

log4j.appender.file=org.apache.log4j.RollingFileAppender
#Here Appender type is defined as RollingFileAppender

log4j.appender.file.File=C:\\Setups\\Logs\\Selenium.txt
#Here the location of file is mentioned where the system generated logs would get saved

log4j.appender.file.maxFileSize=5000KB
#maxFileSize denotes that after every 5000KB a new file will be created, means a single file will not save data more than 5000KB

log4j.appender.file.maxBackupIndex=1
#maxBackupIndex defines that how many backup files will be saved

log4j.appender.file.layout=org.apache.log4j.PatternLayout
#layout is used to set the format of the output file

log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n
#conversionpattern is used to set the conversion type.

log4j.appender.file.Append=false
#do not append the old file. Create a new log file everytime
#Application Logs – This section will help to log the information generated by manual commands in code

log4j.logger.devpinoyLogger=DEBUG, dest1
#Here debug is the Logger level and file is used as an identifier. “devpinoyLogger” string will be passed to getLogger method of Logger class

log4j.appender.dest1=org.apache.log4j.RollingFileAppender

log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3

log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n

log4j.appender.dest1.File=C:\\Users\\Dharmendra\\Toolsqa\\Bookshopee\\src\\automationframework\\test.logs
#Here the location of file is mentioned where the information generated by manual commands in code will get saved.

log4j.appender.dest1.Append=false
#do not append the old file. Create a new log file everytime

//Copy till here

Create a new class log4j.java with steps as:

Right click on “package name” -> new -> class -> log4j -> Click on Finish button

Copy the below code snippet in log4j.properties file:
//Start copying from the next line

package automationframework;
//Write your package name

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Log4j {

private static WebDriver driver = null;

public static void main(String[] args) {

// Create a new instance of the Firefox driver
driver = new FirefoxDriver();

// Hit the URL for which you want to see logs
driver.get(“https://www.google.co.in/”);

// Create reference variable “log” referencing getLogger method of Logger class, it is used to store logs in selenium.txt
Logger log = Logger.getLogger(“devpinoyLogger”);

// Call debug method with the help of referencing variable log and log the information in test.logs file
log.debug(“Getting errors”);

}

}

//Copy till here

Save and Run this Log4j class and check files test.logs and selenium.txt at their locations
It should show the debug logs at both levels.

See Screenshots for both the logs, it should appear like this
i) Selenium Logs – Shows the image of system logs

Selenium Logs

ii) Test Logs – Shows the image of code logs
Test logs

For more reference to log4j visit this link , it has the full documentation of log4j.

Thanks