log4j, a logging library for JavaThere are three ways to configure log4j: with a properties file, with an XML file and through Java code
Within either you can define three main components: Loggers, Appenders and Layouts.
Five standard log4j levels
DEBUG Level
This log4j level helps developer to debug application. Level of message logged will be focused on providing support to a application developer.
INFO Level
This log4j level gives the progress and chosen state information. This level will be generally useful for end user. This level is one level higher than DEBUG.
WARN Level
This log4j level gives a warning about an unexpected event to the user. The messages coming out of this level may not halt the progress of the system.
ERROR Level
This log4j level gives information about a serious error which needs to be addressed and may result in unstable state. This level is one level higher than WARN.
FATAL Level
This log4j level is straightforward and you don’t get it quite often. Once you get this level and it indicates application death.
Log4j Logger Output
When a logger is created, generally you assign a level. The logger outputs all those messages equal to that level and also all greater levels than it. So the order for the standard log4j levels are:
Log4J Levels | TRACE Level | DEBUG Level | INFO Level | WARN Level | ERROR Level | FATAL Level |
TRACE Level | Y | Y | Y | Y | Y | Y |
DEBUG Level | N | Y | Y | Y | Y | Y |
INFO Level | N | N | Y | Y | Y | Y |
WARN Level | N | N | N | Y | Y | Y |
ERROR Level | N | N | N | N | Y | Y |
FATAL Level | N | N | N | N | N | Y |
ALL Level | Y | Y | Y | Y | Y | Y |
OFF Level | N | N | N | N | N | N |
Within either you can define three main components: Loggers, Appenders and Layouts.
Loggers are logical log file names.Each logger is independently configurable as to what level of logging (FATAL, ERROR, etc.) it currently logs.
The actual outputs are done by Appenders.
Appenders use Layouts to format log entries. A popular way to format one-line-at-a-time log files is PatternLayout, which uses a pattern string, much like the C / C++ function printf.
WHY LOGGING....?
Logging helps us in evaluating the cause of code failure.
We need to import log4j jar file into our project for writing logging code.
Logging helps us in evaluating the cause of code failure.
We need to import log4j jar file into our project for writing logging code.
Steps to enable logging in your project:
1.Create a property file having extension properties.Name can be log4j.properties.
Insert the below code in the log4j.properties file:
#Application Logs
#Logger:Responsible for capturing logging Information
log4j.rootLogger=INFO,dest1
#Appender:Responsible for publishing captured logging information to various preferred destinations.
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
#Layout:Responsible for formatting logging information
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}-%t-%x-%-5p-%-10c:%m%n
log4j.appender.dest1.File=C:\\logs\\Application.log
#Do not want to append the old file, want to create a new log file every time then
log4j.appender.dest1.Append=false
Note:
- The level of the root logger is defined as INFO and attaches appender named dest1 to it.
- org.apache.log4j.RollingFileAppender is used to roll files when they reach a defined size.
- Set the maximum size that the output file is allowed to reach before being rolled over to backup files.
- If you want to generate your logging information in a particular format based on a patten then you can use org.apache.log4j.PatternLayout to format your logging information.
d Used to output the date of the logging event. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}.
t | Used to output the name of the thread that generated the logging event. |
p | Used to output the priority of the logging event. |
m | Used to output the application supplied message associated with the logging event. |
n | Outputs the platform dependent line separator character or characters. |
% | The literal percent sign. %% will print a % sign. |
Example
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
public class testLogin {
public static final org.apache.log4j.Logger Log = Logger.getLogger(testLogin.class);
public static void main(String[] args)
{
org.apache.log4j.BasicConfigurator.configure();
PropertyConfigurator.configure("C:\\Users\\Neeraj.Bakhtani\\workspace\\FacebookPOM\\src\\com\\neeraj\\facebook\\properties\\log4j.properties");
LoginPage page=new LoginPage();
LandingPage lp=page.doLogin("testi@gmail.com","password");
Log.info("Website navigated");
}
}
}
No comments:
Post a Comment