Here we learn how to use and configure Log4j for Selenium Web Driver.
Log4j have 3 part
- Logger
- Appender
- Layout or Formatter
This is basically logging the application status.It have 6 level
a.TRACE
b.DEBUG
c.INFO
d.WARN
e.ERROR
f.FATAL
and order of this level is DEBUG < INFO < WARN < ERROR < FATAL means if we set logger as WARN then we get ERROR and FATAL log also.
We set this in properties file like
# Log levels
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC
Here we set INFO as a level for logging.Console,R and HTML are appender we learn later about this.This names are arbitrary.
2.Appender
This means where it writes log.It may write log in console,text file etc.
We set this in properties file like
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=./logs/testlog.log
log4j.appender.TTCC.File=./logs/testlog1.log
# Define the HTML file appender
log4j.appender.HTML=org.apache.log4j.FileAppender
# Path and file name to store the log file
log4j.appender.HTML.File=./logs/application.html
3.Layout or Formatter
It's name clearly defines what it is.It is use to format the output file.This is used how we want to see the output it may be on Console,Text or even HTML.
We set this in properties file like
This is for console layout:
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
This is for text file layout:
# Layout for Rolling File Appender
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
This is for HTML layout:
# Define the html layout for file appender
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
#Define Title of the HTML page
log4j.appender.HTML.layout.Title=Application logs
#Define the log location of application class file
log4j.appender.HTML.layout.LocationInfo=true
How to Configure Log4j:
Step 1:
Add a properties file in our project.
Step 2:
Configure file Should be looks like
# Log levels
log4j.rootLogger=info,CONSOLE,R,HTML,TTCC # Rolling File Appender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender # Layout for Rolling File Appender
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601# Path and file name to store the log file
log4j.appender.TTCC.File=./logs/testlog1.log
log4j.appender.TTCC.MaxFileSize=200KB # Number of backup files
log4j.appender.TTCC.MaxBackupIndex=2# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p (%c:%L:%F) - %m%n# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender# Path and file name to store the log file
log4j.appender.R.File=./logs/testlog.log
log4j.appender.R.MaxFileSize=200KB# Number of backup files
log4j.appender.R.MaxBackupIndex=2# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p (%c:%L:%F) - %m%n# Define the HTML file appender
log4j.appender.HTML=org.apache.log4j.FileAppender# Path and file name to store the log file
log4j.appender.HTML.File=./logs/application.html# Define the html layout for file appender
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout#Define Title of the HTML page
log4j.appender.HTML.layout.Title=Application logs#Define the log location of application class file
log4j.appender.HTML.layout.LocationInfo=true
Step 3:
We need to pass the class name of the getLogger() method for which we take this log.
It looks like :
private static final Logger logger = Logger.getLogger("Log4JSettings");
Step 4:
We need to locate confire file using code by this code
PropertyConfigurator.configure("Log4j.properties");
Step 5:
Add different log in our application where we want in our application.
It looks like :
Sample Code is given below
package seleniumwebdriver;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4JSettings {
private static final Logger logger = Logger.getLogger("Log4JSettings");
public static void main(String args[]) throws IOException {
PropertyConfigurator.configure("Log4j.properties");
try {
/**
* We try to read a File which is not present
* in the path so it get exception which is shown
* in Console Text File as well as HTML Format
* as described in Log4j Properties File. */
FileInputStream fstream =
new FileInputStream("D:\\textfile.txt");
DataInputStream in =
new DataInputStream(fstream);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
}
in.close();
} catch (FileNotFoundException fe) {
logger.error("File Not Found", fe);
logger.warn("This is a warning message");
logger.trace("This message will not be logged since log "
+ "level is set as DEBUG");
} catch (IOException e) {
logger.error("IOEXception occured:", e);
}
}
}
nice article Arjun :)
ReplyDeleteThis comment has been removed by the author.
ReplyDelete