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

Configure Log4j in your project with the help of Log4j.properties 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)   http://logging.apache.org/log4j/1.2/download.html
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 Log4j.properties 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 “Log4j.properties”.

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

log4j.rootLogger=ALL,CONSOLE,R


// Here we define the appender

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.R=org.apache.log4j.RollingFileAppender

#log4j.appender.TTCC=org.apache.log4j.RollingFileAppender

#log4j.appender.HTML=org.apache.log4j.FileAppender


// Here we define log file location

log4j.appender.R.File=./log/testlog.log

#log4j.appender.TTCC.File=./log/testlog1.log

#log4j.appender.HTML.File=./log/application.html


// Here we define the layout and pattern

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

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

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

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

#log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout

#log4j.appender.TTCC.layout.DateFormat=ISO8601

#log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout

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

#log4j.appender.HTML.layout.LocationInfo=true

Step4: Create Sample class where we will use Logger

Once Log4j.properties 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();

                                driver.navigate().to("https://www.google.co.in");





                                log.info("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");

                               

                                driver.close();

                }

 

}
Run the program, you will see below output.
Output:
INFO [main] (LoggerExampleTest.java:16)- This is Info message - Launched Google

DEBUG [main] (LoggerExampleTest.java:17)- This is debug message

ERROR [main] (LoggerExampleTest.java:18)- This is error message

FATAL [main] (LoggerExampleTest.java:19)- This is fatal mesage

TRACE [main] (LoggerExampleTest.java:20)- This is trace message
Based on Log4j.properties 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 -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories