You are on page 1of 6

Log4J

What is log4J- Log4j is free open source tool given by Apache


foundation for creating log files It helps us to generate a log file in
various output target.

Components of log4J
1. Loggers
2. Appenders
3. Layouts

1. Loggers: It is responsible for logging information. To implement loggers into a


project following steps need to be performed -

2. Appenders: It is used to deliver LogEvents to their destination. It decides


what will happen with log information. In simple words, it is used to write the
logs in file. Following are few types of Appenders
1. ConsoleAppender logs to standard output
2. File appender prints logs to some file
3. Rolling file appender to a file with maximum size

Note: In log4j properties we can call appender with any name. There are other
appenders as well but we will restrict to these few.

3. Layouts: It is responsible for formatting logging information in different styles.

The Logger class provides different methods to handle logging activities. It provides
two static methods for obtaining a Logger Object.

 Public static Logger getRootLogger()


 Public static Logger getLogger(String name)

What is Log File?


The log file is a just simple file, which keeps track of the record or event or
info when any event happens or any software run. This whole process is
known as logging. We can create a log file as simple log file as well as
HTML format.
Sample Log file
1- Simple log file in text format

2- Log files in HTML format


 

Why is it required in Selenium?


We can create a log file for our simple script also so we can track
or debug our script easily if anything goes wrong in the script. For
example, if our script is failing at some point then we can track
back what went wrong.
 

How to create log files in selenium


Step 1- Download log4j jar file
Click on the link to
download http://mirrors.ibiblio.org/pub/mirrors/maven/log4j/jars/lo
g4j-1.2.15.jar
 

Step 2- Add log4j to your current project


Select your project > Right click > Click on build path> Click
Configure build path> Go to library section > Add external jar file
> select the log4j > click on save
 

Step 3- Open notepad and copy the below code and save the file
as log4j.properties.

Note- Please create the log folder inside home directory and while
saving use “” to save file

1
2
3
4
5
6
7
8
// Here we have defined root logger
9
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC
1
 
0
// Here we define the appender
1
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
1
log4j.appender.R=org.apache.log4j.RollingFileAppender
1
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
2
log4j.appender.HTML=org.apache.log4j.FileAppender
1
 
3
// Here we define log file location
1
log4j.appender.R.File=./log/testlog.log
4
log4j.appender.TTCC.File=./log/testlog1.log
1
log4j.appender.HTML.File=./log/application.html
5
 
1
// Here we define the layout and pattern
6
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
1
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
7
log4j.appender.R.layout=org.apache.log4j.PatternLayout
1
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
8
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
1
log4j.appender.TTCC.layout.DateFormat=ISO8601
9
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
2
log4j.appender.HTML.layout.Title=Application log
0
log4j.appender.HTML.layout.LocationInfo=true
2
1
2
2
2
3
2
4

Step 4- Write test case


1 import java.util.concurrent.TimeUnit;
2 import org.apache.log4j.Logger;
3 import org.apache.log4j.PropertyConfigurator;
4 import org.openqa.selenium.By;
5 import org.openqa.selenium.WebDriver;
6 import org.openqa.selenium.firefox.FirefoxDriver;
7  
8  
9 public class Google {
1     public static void main(String[] args) {
0       
1     // Here we need to create logger instance so we need to pass Class name for
1      //which  we want to create log file in my case Google is classname
1          Logger logger=Logger.getLogger("Google");
2         
1  
3        // configure log4j properties file
1        PropertyConfigurator.configure("Log4j.properties");
4  
1  
5         // Open browser
1         WebDriver driver = new FirefoxDriver();
6         logger.info("Browser Opened");
1       
7         // Set implicit wait
1         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
8         logger.info("Implicit wait given");
1       
9  
2         // Load application
0         driver.get("https://www.google.co.in/");
2         logger.info("Url opened");
1       
2  
2         // type Selenium
2         driver.findElement(By.name("q")).sendKeys("Selenium");
3         logger.info("keyword type");           
2   }
4 }
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
 

Step 5- Execute  your test case and verify the output and log files
as well
Finally, here are three different log files.
 

Log4j properties

The log4j.properties file is a log4j configuration file which keeps


properties in key-value pairs. By default, the LogManager looks for a file
named log4j.properties in the CLASSPATH.

 The level of the root logger is defined as DEBUG. The DEBUGattaches the


appender named X to it.
 Set the appender named X to be a valid appender.
 Set the layout for the appender X.

log4j.properties Syntax:
Following is the syntax of log4j.properties file for an appender X:
# Define the root logger with appender X
log4j.rootLogger = DEBUG, X

# Set the appender named X to be a File appender


log4j.appender.X=org.apache.log4j.FileAppender

# Define the layout for X appender


log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.conversionPattern=%m%n

log4j.properties Example
Using the above syntax, we define the following in log4j.properties file:

 The level of the root logger is defined as DEBUG, The DEBUGappender


named FILE to it.
 The appender FILE is defined as org.apache.log4j.FileAppender. It writes to
a file named log.out located in the log directory.
 The layout pattern defined is %m%n, which means the printed logging message
will be followed by a newline character.

# Define the root logger with appender file


log4j.rootLogger = DEBUG, FILE

# Define the file appender


log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender


log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

It is important to note that log4j supports UNIX-style variable substitution


such as ${variableName}.

Debug Level
We have used DEBUG with both the appenders. All the possible options are:

 TRACE

 DEBUG

 INFO

 WARN

 ERROR

 FATAL

 ALL

You might also like