Log4j Tutorial
While developing Java/J2EE applications, for debugging an application that is to know the status of a java application at its execution time, in general we use system.out.println statements in the application right…
But we have some disadvantages while using SOPL (system.out.println)statements.
Generally SOPL statements are printed on console, so there are temporary messages and whenever the console is closed then automatically the messages are removed from the console.
It is not possible to store the SOPL messages in a permanent place and these are single threaded model, means these will prints only one by one message on the console screen.
In order to overcome the problems of SOPL statements Log4j came into picture, with Log4j we can store the flow details of our Java/J2EE in a file or databases
This is a Open Source tool given by Apache, for only java projects, to record or write the status of an application at various places.
Working with log4j is nothing but working with classes & interfaces given in 4j.*Log4j is a common tool, used for small to large scale Java/J2EE projects
In Log4j we use log statements rather SOPL statements in the code to know the status of a project while it is executing In real time, after a project is released and it is installed in a client location then we call the location as on-site right, when executing the program at on-site location, if we got any problems occurred then these problems must report to the off showered engineers, in this time we used to mail these Log files only so that they can check the problems easily.
Log4j Jar file download link : 1.2.17 Download Link
Logging Methods:–
➔ debug
➔ error
➔ fatal
➔ info
➔ warn
➔ Trace
Log4j Program :
public class Log4jTutorial {
WebDriver driver;
@Test
public void log4jdemo() {
Logger log= Logger.getLogger(“Google Search”);
PropertyConfigurator.configure(“Log4j.properties“);
driver = new ChromeDriver();
log.info(“Chrome Browser Lanch”);
driver.manage().window().maximize();
log.info(“Browser window maximize”);
driver.get(“https://www.google.com”);
log.info(“Navigate to the Google URL”);
}
}
Log4j Properties Code
// Here we have defined root logger
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC
// Here we define the appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender
// Here we define log file location
log4j.appender.R.File=./log/testlog.log
log4j.appender.TTCC.File=./log/testlog1.log
log4j.appender.HTML.File=./log/logresults.html
// Here we define the layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d – %c -%p – %m%n
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true