Selenium – Logging with Log4j

by seetaram on May 23, 2011

If we are executing Test Suite with hundreds of automated test cases then logging all the events might be useful.

There are two APIs available for logging events.

JUL – Java Logging API

Log4j – Apache Software Foundation

I am going to discuss Log4j as this is an open source logging library as well as it is very much flexible and extensible compared to JUL.  Log4j is built as a subproject of Logging Services Project by the Apache Software Foundation.

Log4j is built with three main concepts: loggers, appenders, and layouts.  Logger is the main engine which sends the logging requests to appender.  Appender might be a console, a log file, printer, etc.  Layout is the formatting of the log output.  I think this is sufficient for this post and for any further information on Log4j you can certainly visit Google and search for Log4j.  On internet, you can get tons of information available for Log4j.

In the following sections configurations of Log4j and also using the logging inside a Selenium test is explained.

Download Log4j from the Apache’s download link:

http:/www.apache.org/dyn/closer.cgi/logging/log4j/1.2.16/apache-log4j-1.2.16.zip

Step 1: Add the Log4j JAR file to the Java Build Path (log4j-1.2.16.jar)

Step 2: Create a new class – Log4jXmlTest

Step 3: Copy the following code into the newly created class

package com.selftechy.junit4;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.log4j.*;
import org.apache.log4j.FileAppender;
import org.apache.log4j.RollingFileAppender;

/**
 * Author: Seetaram Hegde
 */
public class Log4jXmlTest extends SeleneseTestCase {

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

    @Before
	public void setUp() throws Exception {
    	DOMConfigurator.configure("log4j.xml");
    	Log.info("______________________________________________________________");
    	Log.info("Initializing Selenium...");
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
		selenium.start();
		Log.info("Selenium instance started");
	}

    @Test
	public void testAdvancedSearch() throws Exception {
    	Log.info("Opening Google Website");
		selenium.open("http://www.google.com/");
		Log.info("Clicking on advanced search link");
		selenium.click("link=Advanced search");
		selenium.waitForPageToLoad("30000");
		Log.info("Entering search terms");
		selenium.type("as_q", "selenium,selftechy");
		Log.info("Clicking on Advanced Search button");
		selenium.click("//input[@value='Advanced Search']");
		selenium.waitForPageToLoad("30000");
	}

	@After
	public void tearDown() throws Exception {
		Log.info("Stopping Selenium...");
		Log.info("______________________________________________________________");
		selenium.stop();
	}

}

Step 4: Create a new XML file – log4j.xml

Step 5: Copy the following code into it and save (make sure that file is saved as xml file NOT as .txt file)

<?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>

Step 7: Place this log4j.xml file into Project root folder (to find out the path -> right click on the project -> click on properties.  Location shows the project’s root directory).

Path to Project's root folder

Path to Project's root folder

Step 8: Go to Run -> Run As -> JUnit Test

Step 9: It should create a output log file in the project’s root folder with name “logfile.log”

Step 10: Open the log file, following lines should be there in the log file.

2011-05-23 15:30:18,931 INFO [Log4jXmlTest] ______________________________________________________________

2011-05-23 15:30:18,931 INFO [Log4jXmlTest] Initializing Selenium…

2011-05-23 15:30:24,773 INFO [Log4jXmlTest] Selenium instance started

2011-05-23 15:30:24,773 INFO [Log4jXmlTest] Opening Google Website

2011-05-23 15:30:26,850 INFO [Log4jXmlTest] Clicking on advanced search link

2011-05-23 15:30:27,460 INFO [Log4jXmlTest] Entering search terms

2011-05-23 15:30:27,491 INFO [Log4jXmlTest] Clicking on Advanced Search button

2011-05-23 15:30:27,819 INFO [Log4jXmlTest] Stopping Selenium…

2011-05-23 15:30:27,819 INFO [Log4jXmlTest] ______________________________________________________________

 

{ 19 comments… read them below or add one }

Irfan August 1, 2011 at 1:01 pm

I am getting following exception:

log4j:WARN Fatal parsing error 1 and column 15
log4j:WARN Invalid byte 1 of 1-byte UTF-8 sequence.
org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source)
at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipDeclSpaces(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)
at com.selftechy.junit4.Log4jXmlTest.setUp(Log4jXmlTest.java:20)
at junit.framework.TestCase.runBare(TestCase.java:132)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
log4j:ERROR Could not parse file [log4j.xml].
org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)
at com.selftechy.junit4.Log4jXmlTest.setUp(Log4jXmlTest.java:20)
at junit.framework.TestCase.runBare(TestCase.java:132)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
log4j:WARN No appenders could be found for logger (com.selftechy.junit4.Log4jXmlTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:WARN Fatal parsing error 1 and column 15
log4j:WARN Invalid byte 1 of 1-byte UTF-8 sequence.
org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source)
at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipDeclSpaces(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)
at com.selftechy.junit4.Log4jXmlTest.testAdvancedSearch(Log4jXmlTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
log4j:ERROR Could not parse file [log4j.xml].
org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)
at com.selftechy.junit4.Log4jXmlTest.testAdvancedSearch(Log4jXmlTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Reply

seetaram August 4, 2011 at 8:37 am

I think this is because, you directly copied the XML for log4j configuration from the blog. There you need to make some changes such as, first change the double quotes (i.e. you need to again key in the – ” because the one you copied will be in special character format). Second, change the path of the log file as required. ( sometimes after “1.0” after 1.0 one “?” character will be inserted when u copy from the blog that needs to be removed).

Reply

Madhu September 20, 2011 at 12:08 pm

I am not getting “logfile.log” file after sucessful execution of “log4j.xml” file.

Reply

seetaram September 21, 2011 at 6:35 am

I think the log4j.xml file is not properly configured..

Reply

Sneha December 6, 2011 at 10:49 am

Hi,

Can you help me with storing logs in an excel file?
I wanted to know how to mention the cell numbers while logging.

Thanks,
Sneha

Reply

seetaram December 7, 2011 at 9:52 am

No. I don’t think that you can store logs in Excel file.. having said this you can create logs in excel sheet provided you are not using log4j but jxl or poi packages…

I am not sure whether you can create logs in Excel file using log4j

Reply

Subhash April 4, 2012 at 12:00 pm

Hi,

Can we insert IP address in log ? I am using selenium grid with multiple RC’s . In this case I unable to know which one log generatd from which one RC.

Thanks,
Subhash

Reply

seetaram April 16, 2012 at 12:44 pm

Use the below code to get the IP address of the specific system and then insert into the specific log files
InetAddress SysIP=InetAddress.getLocalHost();

SysIP.getHostAddress()

Reply

Harish May 7, 2012 at 8:49 pm

Hi,

I’m using SeleniumRC with TestNG and running my scripts through ant. My requirement is to incorporate the selenium-server
logs in to my application logs (file set in log4j.properties).

Any help here would be very much appreciated.

Here is the code for starting selenium server in build.xml file. I added an arg line for
logging but it doesnt work.

Here is the code in log4j properties file

#Application Logs
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss}
%c %m%n
log4j.appender.dest1.File=(path to the file)\\Application.log
#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=false

Reply

Harish May 7, 2012 at 8:50 pm

Hi,

I’m using SeleniumRC with TestNG and running my scripts through ant. My requirement is to incorporate the selenium-server
logs in to my application logs (file set in log4j.properties).

Any help here would be very much appreciated.

Here is the code for starting selenium server in build.xml file. I added an arg line for
logging but it doesnt work.

Here is the code in log4j properties file

#Application Logs
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss}
%c %m%n
log4j.appender.dest1.File=(path to the file)\\Application.log
#do not append the old file. Create a new log file everytime
log4j.appender.dest1.Append=false

Reply

Siva May 15, 2012 at 4:29 am

Thanks for the above explanation , Can you please provide the datailed steps to create log4j.properties file

Reply

Denis Ballant July 3, 2012 at 9:37 am

Harish,

Here’s the solution to your problem:

java.util.logging.Logger.getLogger(“org.openqa.selenium”).setLevel(Level.WARNING);

I had the same problem and couldn’t find the solution on the net.
In fact Selenium is using the Java Logging facility.
So you just need to disable it using the corresponding API, hence the line of code above.

Cheers,

Denis

Reply

Siva November 17, 2012 at 1:09 pm

Hi Seetaram,

I gone through your post. It was really useful. I configured the same as specified by you.
Now my question is, i want the “logfile.log” file content gets appended.
I need the previously generated logs also in the same file.
For each and every run the log file content should get appended.

Can you provide me the solution for this. So that it will be very much useful for me.

Regards,
Siva

Reply

zerotres July 9, 2013 at 11:00 pm

Add this to the end of your log4j.properties file.

log4j.appender.dest1.Append=true

Reply

Rahul January 14, 2013 at 8:46 am

I am using seleniumn web – drvier.
Have log4j.properties in the root folder of the project.
Below is the code of log4j.properties

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %c{1}:%L [%p] – %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/log/logfile.txt
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %c{1}:%L [%p] – %m%n
log4j.rootLogger=TRACE,console,file

Below is the code in java file :

Logger logger = Logger.getLogger(this.getClass());
logger.info(“Application has launched”);

But still the logs are not created
Do i need to configure anything more ?

Reply

Upasana April 15, 2013 at 9:03 am

Hi,
Using this I am able to generate logfile which is in notepad format.If Iwant to get the logs in Html format,how will I do it?

I tried changing the layoutclass from “” to “” in log4j.xml but no success.

Reply

Upasana April 15, 2013 at 9:04 am

Hi,
Using this I am able to generate logfile which is in notepad format.If Iwant to get the logs in Html format,how will I do it?

I tried changing the layoutclass from “PatternLayout” to “HTMLlayout” in log4j.xml but no success.

Reply

seo December 30, 2013 at 10:11 pm

We are a group of volunteers and opening a new scheme in our community.

Your website offered us with valuable information to work on.
You have done a formidable job and our whole community will be thankful to
you.

Reply

seetaram January 10, 2014 at 6:55 am

Thanks a lot

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email