Log4j is a replacement of System.out.println statements in java.
We will understand why people uses logs in a program.
In Industry whenever we write any method we log below statements like below snap. This helps in debugging of any issues in future.
Now this will be printed with time like Wed Aug 30 18:07:33 IST 2017
This is just to know which test case started at what time.
Now we saw these statements cannot be controlled. I mean we cannot stop them from printing, once written they will be printed.
Formatting of statement cannot be done like first date and time then method name then logging statement. If we want to do we need to write again and again.
This can be easily solved by ‘log4j’.
We can have format defined for logs in log4j at centralize place.
We can disable or enable some category of logs.
Logs can be divided as per there category, explained later.
Try understanding log4j how it is working?
In industry we have categories defined for various logging statement.
Now every company has their own standards to follows. If we have informative things to log then use info.
If we feel something will be needed to debug in future then use debug.
If we are using catch block then go for fatal similarly we need to thing where to use what.
One file is responsible for formatting of a logs and enabling and disabling of above levels is log4j.properties or log4j.xml
Printing to various sources can be done through log4j whereas System.out.println prints only on console.
Appenders and layouts are used for printing to console or sql or to file.
Log4j has three main components (loggers, Appenders and layouts) which works together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.
Here in this example we will create one log file for every package.
Create project structure like below.
Code will be as below.
Run SampleAdmin’s main method and then refresh the project where you will see below shown structure.
Now see both files and see logging statements.
Observe output.
We will be writing here one very simple program for log4j. Read property file and selenium code for the same.
// Property file code
#Use two appenders, one to log to console, another to log to a file
#Set level
log4j.rootCategory = debug, console, file
# Appender which writes to console
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{MM-dd-yyyy HH:mm:ss} %c %m%n
# Appender which writes to a file
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File = E://logs.log
# Defining maximum size of a log file
log4j.appender.file.MaxFileSize = 5mb
log4j.appender.file.MaxBackupIndex = 5
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %d{ISO8601} %5p [%t] %c{1}:%L - %m%n
//Java Code
package com.jbk;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LogDemo {
public static void main(String[] args) {
Logger log = Logger.getLogger("LogDemo");
PropertyConfigurator.configure("log4j.properties");
WebDriver driver = new FirefoxDriver();
log.info("Firefox Opened");
driver.manage().window().maximize();
log.info("Brows Maximized");
driver.get("file:///F:/offline%20website/index.html");
log.info("App Launched");
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("kiran@gmail.com");
driver.findElement(By.xpath(".//*[@id='password']")).sendKeys("123456");
driver.findElement(By.xpath(".//*[@id='form']/div[3]/div/but- ton")).click();
log.info("Login Successful");
String s = driver.getTitle();
System.out.println("Your page Title is:"+s);
}
}
Now go ahead with different types of Appenders and you will get to learn many more things. Try changing the format of output. Learn every line of the properties file. Try enabling and disabling every type of level like info or debug.
This concept is very good to know as as these concepts prove to be useful during an interview and looks good on your profile/resume.