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

 Configure Log4j in your project with the help of Log4j.xml 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 Log4j.xml file (Note: Create Under src folder)
In order to configure Log4j in project, we need to specify various parameters in xml file.
The parameters we need to provide are logger, appenders, layout etc.
These parameters need to create in xml file.
To do this, right click on src folder -> New -> Other -> File -> give the name as “Log4j.xml”.
Now we can see the blank xml file.
Add the blow lines of code in to the xml file. Save the file.
(To learn about how logging can be achieved using log4j.properties file, click here– )
To write this xml file, one should know about 3 Main components of Log4j, different logging levels in Log4j.
Refer the blow links – 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true"

xmlns:log4j='http://jakarta.apache.org/log4j/'>



<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>

</layout>

</appender>



<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">

<param name="append" value="false"/>

<param name="file" value="out/learning.log"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>

</layout>

</appender>



<root>

<level value="INFO"/>

<appender-ref ref="consoleAppender"/>

<appender-ref ref="fileAppender"/>

</root>



</log4j:configuration>

If you want to provide the max size, we can add below statement as param to RollingFileAppender.

<param name="maxFileSize" value="1MB" />
We can define ‘MaxBackupIndex’ option which determines the number of previously rolled files to preserve. MaxBackUp takes only a positive integer value. MaxBackUp value will be set to 1 by default. If we set it to zero, then no roll over occurs and the log file will be simply truncated when it reaches the MaxFileSize.

<param name="MaxBackupIndex" value="2"/>
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.xml file, log messages will be stored in .log file (text file) at the defined path on xml file.

Below screenshot shows the folder location and respective log file.
Hope this helps.
 Related Topic: 

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