May 13, 2019

How to configure log4j in Selenium TestNG framework

Follow below steps to add Log4J Logging to Selenium Framework: 

1. Add below artifacts in POM.xml
<!-- Below Artifacts are for log4j logging! -->
        <!-- -->
        <!-- -->

        <!-- -->

2. Create Log4J file at root directory
  • This file used to place configuration for logging.
  • File contain information such as where to store logs.
  • What will be the log file name.
  • Content appender information(to print logs on console or write in a file.)
  • Rollback policy to rename the previous existing file with timestamp and generate new file with name “prints.log”.
  • Content level information that what to log (Trace/error).

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="" debug="false">
<appender name="fileAppender" class="org.apache.log4j.DailyRollingFileAppender">
   <param name="File" value="./logs/prints.log" />
   <param name="Append" value="true" />
   <param name="Threshold" value="debug" />
   <appendToFile value="true" />
   <param name="DatePattern" value="yyyy_MM_dd_HH_mm"/>
   <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <param name="fileNamePattern" value="./logs/prints_%d{ddMMMyyyy HH:mm:ss}.log"/>
      <param name="rollOver" value="TRUE"/>
   <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{ddMMMyyyy HH:mm:ss,SSS}^[%X{l4j_mdc_key}]^[%c{1}]^ %-5p %m%n" />
<level value="trace"/>
<appender-ref ref="fileAppender"/>

3. Create Class in Common utility package 

 * Logger Documentation:
 * To Log Start test Case: Log.startTestCase("<Test_Case_Name>");
 * To Log End of Test Case: Log.endTestCase("Test_Case_Name>");
 * To Log informative message:"<message>");

package utility;
import org.apache.log4j.Logger;
public class Log {

    // Initialize Log4j logs
    private static Logger Log = Logger.getLogger(Log.class.getName());
    // This is to print log for the beginning of the test case, as we usually
    // run so many test cases as a test suite
    public static void startTestCase(String sTestCaseName) {"*******************************************************************");"*******************************************************************");"$$$$$$$$$$$$$$$$$$$$$" + sTestCaseName + "$$$$$$$$$$$$$$$$$$$$$$$$$");"*******************************************************************");"*******************************************************************");

    // This is to print log for the ending of the test case
    public static void endTestCase(String sTestCaseName) {"XXXXXXXXXXXXXXXXXXXXXXX" + "-E---N---D-" + "XXXXXXXXXXXXXXXXXXXXXX");"X");"X");"X");"X");

    // Need to create these methods, so that they can be called
    public static void info(String message) {;

    public static void warn(String message) {

    public static void error(String message) {

    public static void fatal(String message) {

    public static void debug(String message) {