Showing posts with label Log4j for Selenium. Show all posts
Showing posts with label Log4j for Selenium. Show all posts

Thursday, January 8, 2015

How to create logs in XML format using Log4j

1)Create a project in Eclipse

2)Create a file - log4j.properties and paste the following content in it

# Define the root logger with file appender
log4j.rootLogger = DEBUG, XML

# Define the file appender
log4j.appender.XML=org.apache.log4j.FileAppender
log4j.appender.XML.File=application.xml

# Define the xml layout for file appender
log4j.appender.XML.layout=org.apache.log4j.xml.XMLLayout
log4j.appender.XML.layout.LocationInfo=true
log4j.appender.XML.Threshold=DEBUG


3)Create a sample class to log

(Make sure log4j jar file is added in the build path)

import org.apache.log4j.Logger;

public class Log4j_and_Xml
{
    static Logger log = Logger.getLogger(Log4j_and_Xml.class);

    public static void main(String[] args)
    {
        log.debug("This is debug message");
        log.info("This is info message");
        log.error("This is error message");
        log.fatal("This is  fatal message");
    }
}

Refresh the project to find the application.xml file
That's it, run the program and all logs will be created in XML format

Saturday, January 3, 2015

How to create logs in HTML formats using log4j

1)Create a project in Eclipse
2)Create a file - log4j.properties and paste the following content in it

# Define the root logger with file appender
log4j.rootLogger = DEBUG, HTML

# Define the file appender
log4j.appender.HTML=org.apache.log4j.FileAppender
log4j.appender.HTML.File=application.html

# Define the html layout for file appender
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application logs
log4j.appender.HTML.layout.LocationInfo=true
log4j.appender.HTML.Threshold=DEBUG

3)Create a sample class to log
(Make sure log4j jar file is added in the build path)

import org.apache.log4j.Logger;

public class log4j_and_html
{
    static Logger log = Logger.getLogger(log4j_and_html.class);

    public static void main(String[] args)
    {

        log.debug("Sample debug message");
        log.info("Sample info message");
        log.error("Sample error message");
        log.fatal("Sample fatal message");
    }
}

That's it, run the program and all logs will be created in HTML format


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