Log4j is a review logging structure that gives data about what has occurred during execution. It offers the accompanying favorable circumstances −
- Empowers us to comprehend the application run.
- Log yield can be spared that can be broke down later.
- Aides in troubleshooting, in the event of test robotization disappointments.
- Can likewise be utilized for reviewing purposes to take a gander at the application's wellbeing.
Components
1. Example of Logger class.
2. Log level techniques utilized for logging the messages as one of the accompanying −
- mistake
- caution
- data
- investigate
- log
Example
Let us utilize a similar percent adding machine for this demo.
Stage 1 − Download log4j JAR document from https://logging.apache.org/log4j/1.2/download.html and download the Zipped arrangement of the JAR record.
Stage 2 − Create 'New Java Project' by exploring to the File menu.
Stage 3 − Enter the name of the undertaking as 'log4j_demo' and click 'Next'.
Stage 4 − Click Add External Jar and include 'Log4j-1.2.17.jar'.
Stage 5 − Click Add External Jar and include Selenium WebDriver Libraries.
Stage 6 − Click Add External Jar and include Selenium WebDriver JAR's situated in the Libs organizer.
Stage 7 − Add a New XML document utilizing which we can indicate the Log4j properties.
Stage 8 − Enter the Logfile name as 'Log4j.xml'.
Stage 9 − The last envelope structure is demonstrated as follows.
Stage 10 − Now include the properties of Log4j which would be gotten during execution.
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j = "http://jakarta.apache.org/log4j/" debug = "false">
<appender name = "fileAppender" class = "org.apache.log4j.FileAppender">
<param name = "Threshold" value = "INFO" />
<param name = "File" value = "percent_calculator.log"/>
<layout class = "org.apache.log4j.PatternLayout">
<param name = "ConversionPattern"
value = "%d{yyyy-MM-dd HH:mm:ss} [%c] (%t:%x) %m%n" />
</layout>
</appender>
<root>
<level value = "INFO"/>
<appender-ref ref = "fileAppender"/>
</root>
</log4j:configuration>
Stage 11 − Now for exhibit reason, we will consolidate log4j in a similar test that we have been performing (percent adding machine). Include a class document in the 'Primary' work.
package log4j_demo;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class log4j_demo {
static final Logger logger = LogManager.getLogger(log4j_demo.class.getName());
public static void main(String[] args) {
DOMConfigurator.configure("log4j.xml");
logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
logger.info("TEST Has Started");
WebDriver driver = new FirefoxDriver();
//Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.navigate().to("http://www.calculator.net/");
logger.info("Open Calc Application");
//Maximize the browser
driver.manage().window().maximize();
//Click on Math Calculators
driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();
logger.info("Clicked Math Calculator Link");
//Click on Percent Calculators
driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();
logger.info("Clicked Percent Calculator Link");
//Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar1")).sendKeys("10");
logger.info("Entered Value into First Text Box");
//Enter value 50 in the second number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50");
logger.info("Entered Value into Second Text Box");
//Click Calculate Button
driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input")).click();
logger.info("Click Calculate Button");
//Get the Result Text based on its xpath
String result =
driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b")).getText();
logger.info("Get Text Value");
//Print a Log In message to the screen
logger.info(" The Result is " + result);
if(result.equals("5")) {
logger.info("The Result is Pass");
} else {
logger.error("TEST FAILED. NEEDS INVESTIGATION");
}
logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
//Close the Browser.
driver.close();
}
}
Execution
Upon execution, the log record is made on the root organizer as demonstrated as follows. You CANNOT find the record in Eclipse. You should open 'Windows Explorer' to show the equivalent.
The substance of the document is demonstrated as follows.
