Disable Firefox Logs with Selenium

This is a short article which shows how you can disable Firefox logs. If you have used some newer versions of GeckoDriver with Firefox, you would have observed that there are a lot of low level logs displayed on the console. These default logs are very low level and sometimes look polluted especially when you have set up your own logs in the code. This article works on addressing this issue.

Disable Firefox Logs – How it works

In this article, we will not supress the GeckoDriver/Marionette logging as such. Our code would just redirect this browser level log to a text file, so that the console looks clean. Let us see how this can be done.

What happens when Firefox logs are not disabled

Let us first launch Firefox the usual way, and see what we get in the logs. The code to launch Firefox is given below:

public static void main(String[] args) {		
	System.setProperty("webdriver.gecko.driver","C:\\selenium_drivers\\geckodriver.exe");
	 
	WebDriver driver = new FirefoxDriver();
	driver.get("http://www.google.com");
	System.out.println("Page Title [1] - " + driver.getTitle());
		
	driver.findElement(By.name("q")).sendKeys("Google");
	driver.findElement(By.name("btnK")).click();
	System.out.println("Page Title [2] - " + driver.getTitle());
}

If you use this code, the console logs would look a bit like this:

From the above image, you can see that the page titles you printed is displayed among lots of other low level Gecko/Marionette logs. Let us now see how you can clean up these logs.

Code to disable firefox logs

As we had mentioned previously, we will show you the approach using which this GeckoDriver logs can be moved to a separate text file so that the console looks clean. The code for doing this is given below:

System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"C:\\temp\\logs.txt");

Just use the above 2 lines within your code to redirect the logs to a text file (the location of the text file is given in the second line). The complete code to launch firefox is given below –

public static void main(String[] args) {		
	System.setProperty("webdriver.gecko.driver","C:\\selenium_drivers\\geckodriver.exe");
	System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
	System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"C:\\temp\\logs.txt");
		 
	WebDriver driver = new FirefoxDriver();
	driver.get("http://www.google.com");
	System.out.println("Page Title [1] - " + driver.getTitle());
		
	driver.findElement(By.name("q")).sendKeys("Google");
	driver.findElement(By.name("btnK")).click();
	System.out.println("Page Title [2] - " + driver.getTitle());
}

Run the above code and check the Eclipse console. Please note that it only moves the GeckoDriver logs to the text file. The print statements that you put in your code will get displayed in Console itself.

You can also navigate to the folder location that you provided in your code to see that the Gecko logs are now stored in the text file.

If you don’t provide any absolute path for the text file, then the file will be saved in your project folder itself. For example, if you just give the location as logs.txt, then this logs file will be saved inside your eclipse project folder.

With this we complete our article on how you can disable firefox logs (or rather redirect firefox logs) with Selenium. Please try out this approach and let us know if this works for you.

Do you use any other approach with which you disable firefox logs? If you do, then you can share your approach with us using the comments section.

We share lots of additional information on Facebook, including interesting articles from other blogs as well. Like our page to get all these updates. See you there!!