How to configure log4j in Selenium TestNG framework - Software Testing

Empowering Technology

Post Top Ad

Responsive Ads Here

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! -->
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>


            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>

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="http://jakarta.apache.org/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"/>
   </rollingPolicy>
   <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" />
   </layout>
</appender>
<root>
<level value="trace"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>

3. Create Class Log.java 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: Log.info("<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) {
        Log.info("*******************************************************************");
        Log.info("*******************************************************************");
        Log.info("$$$$$$$$$$$$$$$$$$$$$" + sTestCaseName + "$$$$$$$$$$$$$$$$$$$$$$$$$");
        Log.info("*******************************************************************");
        Log.info("*******************************************************************");
    }

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

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

    public static void warn(String message) {
        Log.warn(message);
    }

    public static void error(String message) {
        Log.error(message);
    }

    public static void fatal(String message) {
        Log.fatal(message);
    }

    public static void debug(String message) {
        Log.debug(message);
    }
}

No comments:

Post a Comment

Thanks a lot for your valuable Comment!

Post Bottom Ad

Responsive Ads Here