Monday, 17 August 2015

Create HTML, Error and Execution logs using log4j -Create more than 2 log files (Core JAVA)

It will create 3 files:

1. HtmlLogReport.html
2. Logfile.log
3. ErrorLog.log

Implement the logger as per your requirement.

Below is the code for log4j.property

# Define the root logger with appender file
log4j.rootLogger = INFO, FILE, rfile
 

# Define the file appender-Logfile.log
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.logfile.File=./TestLogs/Logfile.log

# Define the layout for file appender-Logfile.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.File=./TestLogs/Logfile.log
log4j.appender.FILE.layout.ConversionPattern=%d [%25F:%t:%L] - %m%n

# Define the layout for RollingFileAppender-HtmlLogReport.html
log4j.appender.rfile=org.apache.log4j.RollingFileAppender
log4j.appender.rfile.File=./TestLogs/HtmlLogReport.html
log4j.appender.rfile.MaxFileSize=1000MB
log4j.appender.rfile.Append=true
log4j.appender.rfile.layout=org.apache.log4j.HTMLLayout
log4j.appender.rfile.layout.Title=HTML Log Report
log4j.appender.rfile.layout.LocationInfo=true
# For Second Log File i.e ErrorLog.log
log4j.appender.errorLog=org.apache.log4j.FileAppender
log4j.appender.errorLog.File=./TestLogs/ErrorLog.log
log4j.appender.errorLog.layout=org.apache.log4j.PatternLayout
log4j.appender.errorLog.layout.ConversionPattern=%d [%25F:%t:%L] - %m%n
log4j.logger.errorLog=TRACE,errorLog

JAVA program to implement above log4j.properties


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import reusable.Base;

public class google {
 static Logger log = Logger.getLogger(google.class);
 static Logger elog = Logger.getLogger("errorLog");
 WebDriver driver;
  
  @Test
  public void google1() {
    PropertyConfigurator.configure("src/main/resources/utilities/log4j.properties");
    log.info("Starting first test case");
    driver = Base.getDriver();
    driver.get("http://www.google.com/");
    elog.error("Test Case Fail");      
  }
}

No comments:

Post a Comment