Wednesday, 18 September 2013

Log4j | Selenium

Configure log4j

1| Download log4j.jar  and add into build path
2| Open src folder under project
3| Create a new text file.
4| Paste the following..

#Application Logs
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=\logs\\SeleniumLogs.log
#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=true


5| Save it with an extension, '.properties'. e.g., log4j.properties
6| Now, log through the script as shown below.

public class classname {
private static Logger Log = Logger.getLogger(classname.class);

@BeforeTest
public void setUp() throws Exception {
Log.info("_______started server_______");
Log.warn("Warn");

baseUrl = "https://yourwebsite.com";   
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
   
@Test
public void testAbi() throws Exception {
driver.get(baseUrl + "/web/browse.v");
Log.info("_______Test Inprogress_______");
}
   
@AfterTest
public void tearDown() throws Exception {   
Log.info("_______Stopping server_______");

driver.quit();
}
}


6| Execute the test and check the log file, SeleniumLogs.log under logs folder.

1 comment:

  1. It's not writing anything. I just got a file SeleniumLogs.log. Any suggestions.. I want console output to be displayed in that file..

    ReplyDelete