How to Configure Log4j in your project (Using file) ?

Configure Log4j in your project with the help of file – 

To start with using logger, below steps explain, how can we use Log4j for logging the required messages which can be used later on, might be for debugging propose or is informative

Step1: Download Log4j from below link (Official site)
Log4j is a logging library used in java development. Firstly download Log4j.jar from its official site (Apache)
Step2: Add Log4j.jar to project library
We need to add this jar file in to the build path. To do this, right click on the project -> Build path -> Configure Build path -> Libraries tab -> Add external Jar -> Select Log4j.jar

Now Log4j is added in to your library and we are good use it.
Step3: Create file (Note: Create Under src folder)
In order to configure Log4j in project, we need to specify various parameters in properties file.
The parameters we need to provide are logger, appenders, layout etc.
These parameters need to create in properties file.
To do this, right click on src folder -> New -> Other -> File -> give the name as “”.

Now we can see the blank text file.
Add the blow lines of code in to the properties file. Save the file.
To write this properties file, one should know about 3 Main components of Log4j, different logging levels in Log4j.

Refer the blow links –
Main Components of Log4j

Different Logging levels in Log4j

// Here we have defined root logger


// Here we define the appender





// Here we define log file location




// Here we define the layout and pattern


log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n


log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n




#log4j.appender.HTML.layout.Title=Application log


Step4: Create Sample class where we will use Logger

Once file is created, all is set to write log in your java program.
To achieve this, create sample class.
To define Logger class, use blow syntax
                                Logger log = Logger.getLogger(“your_class_name”);
Import suitable respective jar.
Below sample code shows how can we log the information for various logging level.
Sample Program:
package com.aayushCorp.appData.tests;

import org.apache.log4j.Logger;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;


public class LoggerExampleTest {


                public static void main(String[] args) {


                                Logger log = Logger.getLogger("LoggerExampleTest");

                                WebDriver driver = new FirefoxDriver();


                      "This is Info message - Launched Google");

                                log.debug("This is debug message");

                                log.error("This is error message");

                                log.fatal("This is fatal mesage");

                                log.trace("This is trace message");





Run the program, you will see below output.
INFO [main] ( This is Info message - Launched Google

DEBUG [main] ( This is debug message

ERROR [main] ( This is error message

FATAL [main] ( This is fatal mesage

TRACE [main] ( This is trace message
Based on file, log messages will be stored in .log file (text file) at the defined path on properties file.
Below screenshot shows the folder location and respective log file.
Hope this helps.

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

Get the Framework code at Github Repo: