'How to create log file history in selenium webdriver using java

Currently working on selenium webdriver. I have created a log file and the log information stored in logfile.log. when i start running my test case the log information is captured but the entire history is visible. i want to clear the log file or it need to be cleared before starting the execution of test cases.

Sample log info:

2013-10-01 09:18:06,655 INFO  [TEST] Initializing Selenium... 
2013-10-01 09:18:06,686 INFO  [TEST] Selenium instance started 
2013-10-01 09:18:11,905 INFO  [TEST] ______________________________________________________________ 
2013-10-01 09:18:11,905 INFO  [TEST] Initializing Selenium... 
2013-10-01 09:18:11,921 INFO  [TEST] Selenium instance started 
2013-10-01 09:18:11,921 INFO  [TEST] Accessing Stored uid,pwd from the stored text file 
2013-10-01 09:18:11,921 INFO  [TEST] Retrieved uid pwd from the text file 
2013-10-01 09:18:12,858 INFO  [TEST] Sign in to the OneReports website 
2013-10-01 09:18:12,874 INFO  [TEST] Enter Username 
2013-10-01 09:18:13,077 INFO  [TEST] Enter Password 
2013-10-01 09:18:14,155 INFO  [TEST] Submitting login details 
2013-10-01 09:18:33,139 INFO  [TEST] Stopping Selenium... 
2013-10-01 09:18:33,139 INFO  [TEST]  ______________________________________________________________ 


Solution 1:[1]

I hope this could help you:

Automation Log for Selenium

Step 1- Download log4j jar file

Click on the link to download http://mirrors.ibiblio.org/pub/mirrors/maven/log4j/jars/log4j-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. This file should be placed in the src directory, this should create a "default package".

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

Step 4- Write test case

import java.util.concurrent.TimeUnit;
import orgenter code here.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Google {
   public static void main(String[] args) {

    // Here we need to create logger instance so we need to pass Class name for 
    //which  we want to create log file in my case Google is classname
         Logger logger=Logger.getLogger("Google");

      // configure log4j properties file
       PropertyConfigurator.configure("Log4j.properties");
         // Open browser
        WebDriver driver = new FirefoxDriver();
        logger.info("Browser Opened");

        // Set implicit wait
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        logger.info("Implicit wait given");

        // Load application
     driver.get("https://www.google.co.in/");
     logger.info("Url opened");


       // type Selenium
       driver.findElement(By.name("q")).sendKeys("Selenium");
        logger.info("keyword type");           
    }
}

Step 5- Execute your test case and verify the output and log files as well. This logs will be created in the project folder with "log" label

Solution 2:[2]

You can just clear the log file at the beginning of your code:

PrintWriter writer = new PrintWriter(logfile);
writer.print("");
writer.close();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ALUFTW