Following are the steps
to generate logs for the applications,
Step1: Create one sample project
Step2: Create Package in the project to place java classes
Step3: Download Lo4j jar file from http://logging.apache.org/log4j/1.2/download.html
Step4: Add log4j jar file to the project build path
Step5: Create log4j property file at the project level and add the
properties into the file as shown in fig1.
#All==>all
messages
#WARN==>only
warning message etc..
log4j.rootLogger=ALL
#log4j.logger.<Package>=,<log
file name>FileAppender
log4j.logger.com.pack=,logTestFileAppender
#
junit_log4jFileAppender - used to log messages in the junit_log4j.log file.
log4j.appender.logTestFileAppender=org.apache.log4j.FileAppender
log4j.appender.logTestFileAppender.File=logTest.log
log4j.appender.logTestFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.logTestFileAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %-4r [%t] %-5p %c %x - %m%n
|
Fig 1: log.properties
Step6: Create one java class in the package
Step7: Create reference variable for logger class as shown in
fig2.
public static
Logger logger=Logger.getLogger(classname.class)
|
Fig2: Reference Variable for Logger Class
Step8: In the main method load the logger properties by using configure()
method in PropertyConfigurator Class.
Step9: By using Logger reference variable write info, debug and
error etc types of logs as shown in fig3.
Logger.info(“START:: Starting of
some XY()”);
Logger. debug(“Values passed to
the some XY() are :: a: ”+a);
Logger. error(“ERROR:: Error
occurred in some XY()”);
|
Fig3: Sample code to
write info, debug and error logs
Step10: After completion of code run the project and find the
generated logs in the path given in log.propeties for the property “log4j.appender.logTestFileAppender.File”.
Example:
package com.pack;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LogExample {
public static Logger logger=Logger.getLogger(LogExample.class);
public void test() {
WebDriver driver=new FirefoxDriver();
logger.info("Browser Launched");
driver.get("http://www.gmail.com");
logger.info("URL Entered");
logger.debug("Hi");
logger.fatal("Hello");
}
public static void main(String[] args) {
PropertyConfigurator.configure("log.properties");
LogExample example=new LogExample();
example.test();
}
}
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LogExample {
public static Logger logger=Logger.getLogger(LogExample.class);
public void test() {
WebDriver driver=new FirefoxDriver();
logger.info("Browser Launched");
driver.get("http://www.gmail.com");
logger.info("URL Entered");
logger.debug("Hi");
logger.fatal("Hello");
}
public static void main(String[] args) {
PropertyConfigurator.configure("log.properties");
LogExample example=new LogExample();
example.test();
}
}