66K Views

How to use Log4j with Selenium Web Driver

Log4j is an open source logging framework which store our data related to logs and it is best method that without touching the application binary codes, we can store or update the automation logs of any application.When user have to Run 1000 or above files, it help users to store all log data and the user will get with detailed context for application failures.

Normally what happen when we start Testing any application, first We Run our Test cases and then all our Test script results will print in console. That information could be any detail depends upon purpose. Keeping this mind we use log4j in selenium which help user to understand the test steps or any failure during test execution and also user can update any configuration file without touching the main application. For ex- let’s say you encountered a failure in automation test script and it has to be reported in the system. The set of information that you required is:

  • A complete test steps or Test scenarios.
  • Issues, Description of failures and reason behind failed test case.
  • Time detail for developers to investigate the issue in detail

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:

  •   create object of logger class and in it use log4j(To use Logger class we need to Import org.apache.log4j.Logger).
  •   Define the log level:There are 5 kinds of log level
  1. All – This level of logging will log everything ( it turns all the logs on ).
  2. DEBUG – print the debugging information.
  3. INFO – print informational message that highlights the progress of the application.
  4. WARN – print information regarding faulty and unexpected system behavior.
  5. ERROR – print error message that might allow system to continue.
  6. FATAL – print system critical information which are causing the application to crash.
  7. OFF – No logging.

Appenders – In log4j an output destination is called appender. It allows the destination where the logs get saved i.e manual.logs and selenium.logs. About manual.logs and selenium.logs we will discuss later.

Layouts- It is style to print or saved the logging information in file.

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
   

    b)Manual file- Add a new file named as “manual.logs” with steps as:
Right click on src -> new -> other -> general -> file -> next -> File name as “manual.logs” -> Click on Finish button.

   c)Manual file- Add a new file named as “selenium.logs” with steps as:
Right click on src -> new -> other -> general -> file -> next -> File name as “selenium.logs” -> Click on Finish button.

   Note: These all file should be added to source folder and it is parallel to package.

Copy the below code snippet in log4j.properties file.

This section will help to log the system generated logs in Selenium.txt file.

log4j.appender.file.File=***\\***\\***\\Selenium.logs
Here the location of file is mentioned where the system generated logs would get saved

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

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.File=**\\****\\****\manual.logs
Here the location of file is mentioned where the information generated by manual commands in code will get saved in manual.logs file

Selenium.logs file saved our all Run test results and Manual file saved our log information like for Debug, info, warn, error information.

Steps for using log4j in selenium webdriver.

 Create main class:

  1. Right click on default package -> New -> Class
  2. Give the class name and click on finish.

Copy the below code in main class.

Save and Run this loggingdemo.class and check files manual.logs and selenium.logs 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.

ii) Manual Logs – Shows the image of system logs.

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

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php