10k. Advanced WebDriver – Using Log4j Part 1

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,

  1. What the test case is actually doing
  2. How the application has responded to our code
  3. Exceptions, issues, failures, errors, etc. can be traced down to its root when logged properly with customized error messages
  4. 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
  5. 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

  1. Popular Java-based logging package developed in 1996
  2. Distributed under the Apache Software License and hence it is open source
  3. Has various levels of logging – allows us to control the volume of logged output
  4. 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.

  1. OFF – turns logging off
  2. FATAL – severe errors leading to application termination
  3. ERROR – unexpected situations and runtime errors
  4. WARN – warning messages
  5. INFO – gives information about some interesting runtime events
  6. DEBUG – most commonly used log level, provides detailed information on the system flow
  7. TRACE – information with highest level of detail

Log4j components

There are three main components,

  1. Loggers

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.

  1. Appenders

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.

  1. Layouts

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

Log4j configuration

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.

Environment Setup

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.

download Apache Log4j

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.

JAR selection window

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,

Log4j.properties file creation

Ignore the three extra packages you see for now as I have created them for my coding purposes.

Log files

  • 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’
  1. system.log – will have all the logs generated by the system
  2. test.log – will contain all the logs generated as a result of the manual commands provided by the user

Log files created

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.

Lakshmi Chandana is a Software professional + passion-fueled blogger + novel-reader + artist + tutor to make your day a little brighter than it was before! She is thrilled you are here! She is on a mission to make sure learning sticks but with the fun part kept intact. She uses certain tricks called BrainBells (inspired from barbells and dumbbells used for workout) to achieve this and she says, “this is not the hardest job! As once minions said, it for sure is working in a bubble wrap factory. Imagine the self-control needed!” So, dive in to explore the fun-filled World of Learning!!

2 thoughts

  1. Hello Chandana,

    Thank you for sharing your knowledge. I am enjoying learning from your blog. They are put in a very simple to understand language.
    Your tutorial have become one of my regular source of learning and for clarifying my doubts.

    Please keep up the good work.
    Thank you.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search