package testlearn;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;

public class Log4jTutorial {

WebDriver driver;

@Test
public void log4j() throws Exception {

Logger log= Logger.getLogger(“Techlearn.in”);
PropertyConfigurator.configure(“Log4j.properties”);
//System.setProperty(“org.apache.commons.logging.Log”, “org.apache.commons.logging.impl.Jdk14Logger”);

System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium\\chromedriver.exe”); // Your chromedriver path.
driver=new ChromeDriver();
log.info(“Chrome Browser Opened”);

Thread.sleep(2000);
log.info(“Set wait for a page to load time is 2 seconds”);

driver.manage().window().maximize();
log.info(“Browser window maximized”);

driver.get(“http://techlearn.in/”);
log.info(“Techlearn.in Website is launched”);

driver.findElement(By.xpath(“//*[@id=\”menu-item-449\”]/a/span”)).click();
log.info(“Click On About Us on Main menu bar”);

driver.navigate().to(“http://www.google.com”);
log.info(“Browser navigate to Google Search Page”);

driver.findElement(By.name(“q”)).sendKeys(“Techlearn.in”);
log.info(“Search on the Techlearn.in google search field”);

driver.close();
log.info(“Succesfully program run and Browser is closed”);

}

}

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/application.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

Leave a Reply