Saturday, August 30, 2014

Log4j for Selenium

Apache log4j is a Java-based logging utility.
Logging helps us collect information about how the application is running and also helps us debug if any failure occurs.
With log4j it is possible to enable logging at runtime
When logging is wisely used, it can prove to be an essential tool.

There are many places you can use Log4j,
log4c - It is C based logging library
log4js - Log4js logs the events of the browser remotely on the server, with Ajax you can send logs in XML or JSON format as well
Apache Log4net - Logging for .NET framework
Similarly we have log4perl, PL-SQL-Logging-Utility and Log4db2 providing logging capability for all types of applications

Here is an example for Log4j with JAVA

1)Create a project and add an xml file log4j.xml to it and paste the following contents
Remember, log4j should be at project level, to make it available for all your scripts

<?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.FileAppender">

<param name="Threshold" value="INFO" />

<param name="File" value="logfile.log"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />

</layout>

</appender>

<root>

<level value="INFO"/>

<appender-ref ref="fileAppender"/>

</root>

</log4j:configuration>

2) Understnading the log4j.xml

<appender name="fileAppender" class="org.apache.log4j.FileAppender">
This line will ensure your logs will get appended each you run, rather than creating a new log file for your each run

<param name="File" value="logfile.log"/>
This line will ensure it creates a file by name logfile.log at project level for all logs

<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
Tells in which way you want to print your logs,

Ex
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=
      %d{yyyy-MM-dd}-%t-%x-%-5p-%-10c:%m%n

 Will create log as

 2010-03-23-main--DEBUG-log4j :

You can find more info on pattern mataching in the below link
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

3)In your Framework's base script add the line

 private static Logger Log = Logger.getLogger(Log4j.class.getName());
 DOMConfigurator.configure("log4j.xml");

 Then, in each of the scripts you can use Log.info and Log.error

Ex

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;


public class Basics_Log4j {


private static Logger Log = Logger.getLogger(Basics_Log4j.class.getName());


public static void main(String[] args) {

DOMConfigurator.configure("log4j.xml");

        Log.info("Printing Info Log from Log4j using Log.info");
        Log.debug("Printing Debug Log from Log4j using Log.debug");
        Log.error("Printing Error Log from Log4j using Log.error");
        Log.trace("Printing Trace Log from Log4j using Log.trace");
}

}

Output in logfile.log

2014-08-30 16:29:21,116 INFO  [Basics_Log4j] Printing Info Log from Log4j using Log.info
2014-08-30 16:29:21,116 ERROR [Basics_Log4j] Printing Error Log from Log4j using Log.error




No comments:

Post a Comment