How to log messages in webdriver

3,904 views
Skip to first unread message

remya

unread,
Apr 5, 2012, 7:05:27 AM4/5/12
to webdriver
Hi All,


How do i use Logger class for logging messages into a file during
automation?

Krishnan Mahadevan

unread,
Apr 5, 2012, 7:06:54 AM4/5/12
to webd...@googlegroups.com
Remya,
Can you please help elaborate what you are trying to achieve ?
Are you looking at redirecting the webdriver logging stubs into a file, or are you trying to log your testcase logs into a file ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


SantoshSarma

unread,
Apr 9, 2012, 3:30:43 AM4/9/12
to webd...@googlegroups.com
Yes Krishnan, I want to print my webDriver logs in separate file. 

How can I achieve that ? Is there any class/method in WebDriver to print logs?

 Thanks & Regards,
-SantoshSarma

Raja sankar

unread,
Apr 9, 2012, 4:59:21 AM4/9/12
to webd...@googlegroups.com
In Webdriver, logging will be enabled default. Level is FINE.

If you use logger, you'll get the logs.

If you want to set the level, you can set using this,

((RemoteWebDriver) driver).setLogLevel(Level.ALL);

or in the logger itself

logger.setLevel(Level.ALL);

both will produce the same level of logs.

ராஜசங்கர்
(Rajasankar)

remya

unread,
Apr 10, 2012, 11:07:25 AM4/10/12
to webdriver
@Krishnan

I want to the webdriver logs in a separate file.Also in which part of
the program should i write the code for this?
(is it in the setup() method?)

On Apr 5, 12:06 pm, Krishnan Mahadevan

Krishnan Mahadevan

unread,
Apr 10, 2012, 11:28:53 AM4/10/12
to webd...@googlegroups.com
Remya,
You should probably do something like this (preferably in a setup() method)

Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
Handler handler = new FileHandler("C:/Users/krmahadevan/logs.txt", true);
Formatter newFormatter = new SimpleFormatter();
handler.setLevel(Level.ALL);
handler.setFormatter(newFormatter);
logger.addHandler(handler);
RemoteWebDriver.setLogLevel(Level.ALL);

PS : I dont have extensive knowledge on Logging but I was able to get the RemoteWebDriver to append its logs to my logs.txt file.

Hope that gets you started.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


remya

unread,
Apr 10, 2012, 12:39:32 PM4/10/12
to webdriver
Thanks a lot Krishnan ,its working for me!
Thanks All...

On Apr 10, 4:28 pm, Krishnan Mahadevan
<krishnan.mahadevan1...@gmail.com> wrote:
> Remya,

Naresh

unread,
Aug 1, 2012, 9:33:19 AM8/1/12
to webd...@googlegroups.com
Hello all,

Is that 'Logger' class from the Selenium framework or is it from another framework?

Thanks,
Naresh
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/webdriver?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "webdriver" group.
> > To post to this group, send email to webd...@googlegroups.com.
> > To unsubscribe from this group, send email to

Manoj Kapuganti

unread,
Aug 1, 2012, 9:58:10 AM8/1/12
to webd...@googlegroups.com
Hello Guys,

As Krishnan said, I am trying to use code like this

Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
Handler handler = new FileHandler("E:/Terra Dotta Selenium Automation/logs.txt", true);
Formatter newFormatter = new SimpleFormatter();
handler.setLevel(Level.ALL);
handler.setFormatter(newFormatter);
logger.addHandler(handler);
RemoteWebDriver.setLogLevel(Level.ALL);

Please post the classes that need to import.

Its not working for me with below classes.

Error on Eclipse is "Cannot make static reference to the non-static method setLogLevel(Level) from the type RemoteWebDriver"

Rightnow I am using

import java.util.concurrent.TimeUnit;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.testng.annotations.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.WebDriver;

Best Regards,
Manoj Kapuganti

Krishnan Mahadevan

unread,
Aug 1, 2012, 10:10:47 PM8/1/12
to webd...@googlegroups.com

Manoj,

The error clearly states the problem: 

Error on Eclipse is "Cannot make static reference to the non-static method setLogLevel(Level) from the type RemoteWebDriver"

I think now seLogLevel is no longer a static method but instead an instance method. 

Replace RemoteWebDriver.setLogLevel() with driver.setLogLevel() where driver is an instance of RemoteWebDriver. 
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/NGSQoN3AfZ4J.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

Manoj Kapuganti

unread,
Aug 2, 2012, 1:15:03 AM8/2/12
to webd...@googlegroups.com
Hello Krishnan,

I have replaced with driver.setLogLevel(Level.ALL);

But setLogLevel method is undefined for type Webdriver.

Problem not resolved Krishnan.

I would be good if you post the code with classes to import.

Best Regards,
Manoj Kapuganti

Krishnan Mahadevan

unread,
Aug 2, 2012, 1:32:40 AM8/2/12
to webd...@googlegroups.com
Manoj,
I am not sure, as to why you would be facing problems. I clearly mentioned that driver has to be of type RemoteWebDriver, because that is where the setLogLevel method has been defined and I dont think it is part of the WebDriver interface.

Here's a full fledged working code that works fine for me !


package raw.selenium;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FetchLogsFromRemoteWebDriverTest {

private RemoteWebDriver driver;
DesiredCapabilities dc;
private String hubURL = "http://localhost:4444/wd/hub";
private String logFileName = "C:/Users/krmahadevan/logs.txt";

@BeforeClass
public void setup() throws SecurityException, IOException {
dc = new DesiredCapabilities();
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
driver = new RemoteWebDriver(new URL(hubURL), dc);
Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
Handler handler = new FileHandler(logFileName = "C:/Users/krmahadevan/logs.txt", true);
SimpleFormatter newFormatter = new SimpleFormatter();
handler.setLevel(Level.ALL);
handler.setFormatter(newFormatter);
logger.addHandler(handler);
driver.setLogLevel(Level.ALL);
}

@Test
public void f() {
driver.get("http://www.google.com");
}

@AfterClass
public void tearDown() throws IOException {
driver.quit();
File file = new File(logFileName);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuffer sb = new StringBuffer();
String eachLine = null;
while ((eachLine = br.readLine()) != null) {
sb.append(eachLine).append("\n");
}
br.close();
System.out.println("Log contents : \n" + sb.toString());

}
}



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/LljhGtUG8JEJ.

Manoj Kapuganti

unread,
Aug 2, 2012, 6:49:42 AM8/2/12
to webd...@googlegroups.com
Hello Krishnan,

Thanks for your response.

Sorry to say that I am not using RemoteWebDriver as not using Grid.

I am using TestNG without Grid. In my code for creating a driver object, we use Webdriver driver = new FirefoxDriver();

I am unable to use above code. Help me.

Best Regards,
Manoj Kapuganti

Naresh

unread,
Aug 2, 2012, 7:03:54 AM8/2/12
to webd...@googlegroups.com
Hello Manoj,

You should be able to typecast your FireFoxDriver instance to RemoteWebdriver instance.

FirefoxDriver is a derived type from the RemoteWebdriver.

Thanks,
Naresh

Mattayi

unread,
Aug 9, 2012, 6:58:47 AM8/9/12
to webdriver
@Krishnan Mahadevan

I used the same code provided above. But the log file is empty. My jar
version is 2.25. Do i have to add anything more.

Note : It is working properly in jar version 2.15.
> > *Best Regards,*
> > *Manoj Kapuganti*

Thanks
Mathew
Message has been deleted

Manoj Kapuganti

unread,
Aug 9, 2012, 10:37:38 AM8/9/12
to webd...@googlegroups.com
Hello  Mathew ,

Please try to use below code.

        Logger logger = Logger.getLogger("");
FileHandler fh;
fh = new FileHandler(Path where to create the log)
logger.addHandler(fh);
logger.setLevel(Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);

Classes required are:

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

Best Regards,
Manoj Kapuganti

susanth.nair

unread,
Aug 9, 2012, 11:04:17 AM8/9/12
to webd...@googlegroups.com
remya,


Either
 
import java.util.logging.Level;
import java.util.logging.Logger;
 
 
public class MyClass {
   final static myLog = Logger.getLogger(MyClass.class.getName());
 
publicstatic blabla{
        if (myLog.isLoggable(Level.INFO)) {
            myLog.info("log it");
        }
}
 
 
 
Or (use Log4j)

 

 

import org.apache.log4j.Logger;
 
public class MyClass {
 
  static Logger myLog = Logger.getLogger(MyClass.class.getName());
 
publicstatic blabla{
   
     myLog.debug("log it);
 
}
}




Susanth Nair

Automate Automation.....

Mattayi

unread,
Aug 10, 2012, 4:44:48 AM8/10/12
to webdriver
@ Manoj

Thanks it is working

Mike Riley

unread,
Sep 21, 2012, 12:47:10 PM9/21/12
to webd...@googlegroups.com
That is another topic, so please start a new thread.

Mike

On Thursday, September 20, 2012 2:57:25 PM UTC-7, sudha joshna wrote:
How can I capture the logs if I am using seleniumdotnet package.I cannot find Logger class in there,any ideas?
I want to see all the selenium commands that get executed in my test cases in a log file.

Sudha
Reply all
Reply to author
Forward
0 new messages