Log4j Configuration file

After executing our basic test case using logger in log4j, we would modify it, to include "Appender" and "layout" objects by implementing "Configuration file" in our code.

What is Configuration file?
Configuration files are used to configure settings of log4j file. This can bewritten in XML or in Java properties (key=value) format.


Example:-
Using Basic configuration file in the code, which is used to create simple log4j setup.
Syntax
BasicConfigurator.configure();


If we run the same code that we used on our first test case and add BasicConfigurator it would look like this:-

package Log4j_Learning;
import org.apache.log4j.Appender;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
public class Log4j_FirstTestCase {
//Creating the logger object
static Logger log = Logger.getLogger(Log4j_FirstTestCase.class);
public static void main(String[] args) {
//log.setAdditivity(false);
BasicConfigurator.configure();
//Setting the log level
log.setLevel(Level.WARN);
//Creating the layout object
Layout sLayout = new SimpleLayout();
//Creating the Appender object
Appender app = new ConsoleAppender(sLayout);
//Adding appender to logger
log.addAppender(app);
log.debug("First debug Message");
log.info("First info Message");
log.warn("First Warning Message");
log.error("First Error Message");
log.fatal("First Fatal Message");
}
}

Output which varies with log.setadditivity(false) is applied:-

setadditivity to avoid repetitive logs in log4j
log4j, logs with/without setAdditivity

Note:-We are having double results in the console,it's because appenders are not singletons, they are additive.Meaning, A category inherits all the appenders from its ancestors (by default). If we add an appender to a category and it writes to the same underlying stream (console, same file etc.) as some other appender, the same log message will appear twice (or more) in the log. In addition, if two categories in a hierarchy are configured to use the same appender name, Log4j will write twice to that appender.


To avoid such situation we need to Use log.setAdditivity(false) on a category to disable inheriting of appenders. Then, log messages will only be sent to the appenders specifically configured for that category.


Files Required:-
log4j_Selenium.Java [Test Case with logs]
log4j.xml [xml Configuration file, we can use properties file also]
log4j_logfile.txt[File for writing logs]
log4j example file structure
Project Structure for log4j test case.


Java Code:-
package Log4j_Learning; 
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class log4j_Selenium {
//Creating the logger object
static Logger log = Logger.getLogger(log4j_Selenium.class);
public static void main(String[] args)
{
DOMConfigurator.configure("log4j.xml");
log.info("**************Begining of Logs******************");
//Creating WebDriver Object
log.info("Launching the Browser");
WebDriver driver = new FirefoxDriver();
//Opens the given URL
driver.get("http://www.uftHelp.com");
log.info("Fetching the Title");
//Returns the Title of Current Page
String sTitle = driver.getTitle();
log.info("My First Selenium Program using Log4j");
log.info("Title is = '"+sTitle+"'" );
//Closing the Browser
driver.close();
log.info("Browser closed");
System.out.println("Logs Created Successfully");
log.info("**************Ending of Logs*********************");
}
}


XML file:-
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- Creating the File Appender -->
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<!-- File to write logs -->
<param name="File" value="log4j_logfile.log"/>
<!-- Layout = "PatternLyaout" -->
<layout class="org.apache.log4j.PatternLayout">
<!-- Printing message with date , time & class name -->
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>


Output:-
Open the log4j_logfile,which will be created in the project folder.
Output of log4j in text file

Explanation:-
We used the root logger to create a "Info" level message and used the "File Appender" to paste the results into the external text file(log4j_logfile).Furthermore we have used layout as "Pattern Layout" to create a pattern of message in the form of "%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n", which means date time + log level + message.
log4j structure
Configuration file structure


Note:- Incase we want results to be shown in the "Console" of the IDE(Eclipse), we can use "Console Appender" in our Configuration file(log4j.xml).
Just change the configuration file for the above java code and check the output in the console and same external file.


Configuration file with Console+file Appenders:-
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- Creating the Console Appender -->
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- Printing message with date , time & class name -->
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<!-- Creating the File Appender -->
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<!-- File to write logs -->
<param name="File" value="log4j_logfile.log"/>
<!-- Layout = "PatternLyaout" -->
<layout class="org.apache.log4j.PatternLayout">
<!-- Printing message with date , time & class name -->
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="fileAppender"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>