Logging with Log4J

Log4j is widely used API for generating the logs and there are few steps to configure it with Selenium Webdriver.

  • Download the latest log4j api from here.
  • Include the log4j-1.2.XX.jar file in your project build path.
  • Right click on your project > New > Other…>XML>XML FIle> and create a XML file with the log4j.xml name.
  • Copy and paste the below xml contents in newly created XML file-

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>
<log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/&#8221; debug=”false”>
<appender name=”fileAppender” class=”org.apache.log4j.FileAppender”>
<param name=”Threshold” value=”INFO” />
<param name=”File” value=”logfile.log“/>
<param name=”append” value=”true”/>
<layout class=”org.apache.log4j.PatternLayout”>
<param name=”ConversionPattern” value=”%d %-5p [%c{1}] %m %n” />
<level value=”INFO”/>
<appender-ref ref=”fileAppender”/>

  • Create new class file without main method and paste below code-

public class TestCase_Logging {
public static WebDriver driver=null;
private static Logger Log = Logger.getLogger(TestCase_POF.class.getName());
public static void main(String[] args) {
System.setProperty(“webdriver.ie.driver”, “D:/IE_Driver/IEDriverServer.exe”);
driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Log.info(“Start the application.”);


Note: Highlighted codes are important to generate the log, so don’t miss them.

  • Now run the program and go to Project root folder. System has been generated logfile.log file.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: