Log4j Integration with Selenium

Let’s see, Log4j Integration with Selenium. In this tutorial, We will learn How can we generate the logs in Selenium.

In Automation, reporting plays an important role so we can generate the different type of HTML reports, XSLT report, log files etc. For Log generation, we will use Log4j in our scripts.

 

Why is Log4j required in Selenium?

 We create the log file to track or debug our script easily if anything goes wrong in the script. For example, if our script is failing at some point then we can track back what went wrong.

 

What is Log4J?

Log4j is free open source tool given by Apache foundation for creating log files It helps us to generate the log file in various output target.

 

you can download the Log4j jar file from here! (Download Log4j Jar)

From here download the .zip file from the Mirrors section. Unzip this and include this into your project using Add External Jar using Build path.

 

Log4j has three principal components-

  • Loggers
  • Appenders
  • Layout

 

Loggers

Loggers are responsible for logging information. To implement loggers into a project following steps need to be performed following steps –

  • Create an instance for logger class inside the class for which we want to create the logs.

  • Define the log level. Primarily there are many 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

 

Appenders

Appender is used to write the logs in the file. Following are few types of Appenders

  • ConsoleAppender logs to standard output
  • File appender prints log to some file
  • Rolling file appender to a file with maximum size

 

Layouts

The layout  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.

  • Public static Logger getRootLogger()

  • Public static Logger getLogger(String name)

 

How to Configure Log4j in Project

First, you have to download the log4j jar files and add these jars in your project as we have discussed in this tutorial earlier.

 

To configure log4j, first, 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. Testing.logs

    • Second: Will write the information generated by manual commands in code into the file name- Manual.logs

  • Layout will be PatternLayout

 

#RootLogger

 

log4j.rootLogger=DEBUG,file, INFO

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=D:\\Logging\\Testing.logs

log4j.appender.file.maxFileSize=2MB

log4j.appender.file.maxBackupIndex=5

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c<strong><strong>{1}</strong></strong>:%L – %m%n

log4j.appender.file.Append=false

 

 

#ApplicationLogger

 

log4j.logger.devpinoyLogger=DEBUG, dest1

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

log4j.appender.dest1.maxFileSize= 5MB

log4j.appender.dest1.maxBackupIndex=6

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=D:\\Logging\\Manual.logs

log4j.appender.dest1.Append=false

 

In the above code, we have configured log4j to log in two different files named as Testing.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 the file reaches this size, a new file will be created with the same name and the old file name will be added as an Index to it.

  • “maxBackupIndex” is used to configure a 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, then every time a new file will be created rather than old file will be used for logging

 

Now do one thing copy above code and paste it in notepad and save the file aslog4j.properties and Create two more files inside the Logging directory in C Driver ( the path we have mentioned in the Properties file)and give them names such as Testing.logs and Manual.logs. These files will contain all the logs created by the system and manually logged statements.

 

Now copy-paste this log4j.properties file inside the src file inside your project.

Now create the java file for which you want the logging. Inside the Java, file instantiates the Logger and write the log using this Logger object. You can refer the below written code –

 

Run the above code and it will generate the logs inside the Testing.log file. Using this we can generate the log files for our project. When you want to print the exception in logs if occur then you can write the exception message inside the log like this.

 

This will print the whole error in the Testing.logs file. You can open and read the logs file usingNotepad++ on windows.

 

In case you are using Linux/Unix system then go to desired Directory i.e Logging and run the command on terminal –

tail -1000f Testing.logs

 

This is how we can configure the log4j in our project and enable the logging. If you have any concern or query then please let us know. We will help you.

0 Comment

Leave a Reply