Log4j Configuration file – User friendly Tech help

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?
nConfiguration files are used to configure settings of log4j file. This can bewritten in XML or in Java properties (key=value) format.
n
nExample:-
nUsing Basic configuration file in the code, which is used to create simple log4j setup.
nSyntax
nBasicConfigurator.configure();
n
nIf we run the same code that we used on our first test case and add BasicConfigurator it would look like this:-

n

package Log4j_Learning;

n

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"); }}

n

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

n

n

n

n

n

n

n

n

log4j, logs with/without setAdditivity

n

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.
n
nTo 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.
n
nFiles Required:-

nlog4j_Selenium.Java [Test Case with logs]
nlog4j.xml [xml Configuration file, we can use properties file also]
nlog4j_logfile.txt[File for writing logs]n

n

n

n

n

n

n

n

Project Structure for log4j test case.

n

nJava Code:-

n

package Log4j_Learning; 

n

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*********************"); }}

n

nXML file:-

n



















n

nOutput:-
nOpen the log4j_logfile,which will be created in the project folder.

n

n

n

n

n

n

n

n

Output of log4j in text file

nExplanation:-
nWe 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.n

n

n

n

n

n

n

n

Configuration file structure

n

nNote:- 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).
nJust change the configuration file for the above java code and check the output in the console and same external file.
n
nConfiguration file with Console+file Appenders:-

n



























Was this article helpful?
YesNo

Similar Posts