selenium-logger icon indicating copy to clipboard operation
selenium-logger copied to clipboard

A basic wrapper to get useful information from Selenium Logging

Selenium Logger

The most useful information for Selenium users gets quickly buried in the logs by default.

This package is intended to provide a means to quickly get the most useful information...

If you have...

  • :bug: a bug — let me know by Creating an Issue!
  • :bulb: an idea — if it helps everyone, Create a PR!
  • :thinking: a different opinion — if this library does something that you don't like, copy/paste the code to your own project and edit it however you want!
  • :rocket: a different implementation — if there's a better way to do things than this library, create your own! (I'll even help you get it published if you want)

(all made possible because this library uses the MIT License)

Overview

This project allows you to manage:

  • Selenium client logs
  • Driver logs

This project does not manage:

Selenium Logs

Selenium logs output using the JUL (Java Util Logging) Logger implementation. This selenium-logger library works directly with JUL code to provide the output you want with only a couple lines of code and no configuration files.

If you are already working with logging in your project or other libraries and want to access Selenium logs without requiring this project, see the Alternatives section below.

Driver Logs

The driver logging output is generated by the diver binaries. This library allows you to change the things the drivers allow you to change, typically including levels and location. See below for more information on the differences between each of the browser drivers.

This functionality was not fully supported in Chrome and Edge before Selenium 4.12.1

Geckodriver logs used to be turned on and sent to console by default, which created a lot of noise. As of Selenium 4.9.1, logs are now turned off by default.

Installation

For Maven users, add this dependency to pom.xml

<dependency>
  <groupId>com.titusfortner</groupId>
  <artifactId>selenium-logger</artifactId>
  <version>2.3.0</version>
</dependency>

Selenium Usage

Log Level

By default, Java log level is set to Level.INFO.

Almost everything of interest in Selenium is currently logged with Level.FINE, so using enable() changes the level to FINE:

SeleniumLogger.enable();

To set a specific level (e.g., you want to avoid info logs and just see warnings)

SeleniumLogger seleniumLogger = new SeleniumLogger();
seleniumLogger.setLevel(Level.WARNING);

Log Output

By default, logs are sent to the console in System.err. If you want to store the logs in a file, add this to your code:

SeleniumLogger seleniumLogger = new SeleniumLogger();
File logFile = new File("/path/to/selenium-log.txt");
seleniumLogger.setFileOutput(file)

Log Filtering

By default, all Selenium classes are logged, except for the Open Telemetry classes because Grid Observability is out of scope for this project.

To create a list of specific lasses to turn on logs for, based on logger name :

SeleniumLogger.enable("RemoteWebDriver", "SeleniumManaager")

Advanced Logging

  • Disable all logging with SeleniumLogger.disable()
  • Turn on logging for all classes, all levels with SeleniumLogger.all()
  • Create and use your own formatter with setFormatter()
  • Create and use your own filter with setFilter()

Alternatives

If you are already doing logging in your project or are using other libraries that require SLF4J (Simple Logging Facade for Java), you can access Selenium's JUL output with jul-to-slf4j. With that library and the logback-classic library, you can add Selenium's logger names as desired to the src/main/resources/logback.xml file. Note that this is only an alternative to Selenium logging, and is independent of the driver logging solutions described in the next section.

Driver Usage

These methods will turn on driveer logging;

enable() method sets logger level to INFO and output to System.err

ChromeDriverLogger.enable();
EdgeDriverLogger.enable();
GeckoDriverLogger.enable();
InternetExplorerDriverLogger.enable();
SafariDriverLogger().enable();

all() method sets logger level to lowest level and output to System.err

ChromeDriverLogger.all();
EdgeDriverLogger.all();
GeckoDriverLogger.all();
InternetExplorerDriverLogger.all();

Log Level

To set a specific level, or to change an existing log level use the setLevel() method. Setting a level ensures output is sent to System.err by default.

To change to a custom level:

new ChromeDriverLogger().setLevel(ChromiumDriverLogLevel.DEBUG);
new EdgeDriverLogger().setLevel(ChromiumDriverLogLevel.DEBUG);
new GeckoDriverLogger().setLevel(FirefoxDriverLogLevel.DEBUG);
new InternetExplorerDriverLogger().setLevel(InternetExplorerDriverLogLevel.DEBUG);

Log Output

Sending output to a file ensures level is set to level INFO, without needing to call enable().

To send output to a file:

new ChromeDriverLogger().setFile(new File("chromedriver.log"));
new EdgeDriverLogger().setFile(new File("edgedriver.log"));
new GeckoDriverLogger().setFile(new File("geckodriver.log"));
new InternetExplorerDriverLogger().setFile(new File("iedriver.log"));

Chromium Specific Logging

  • Append to a log file instead of rewriting it with appendToLog()
  • Set readable timestamps with setReadableTimestamp()

Firefox Specific Logging

Firefox truncates long lines in the log by default. This can be turned off with: new Geckodriver().setTruncate(false)

Safari Specific Logging

Safari is special. It does not log to console, only to a file. The file has a unique name per session and is stored in "~/Library/Logs/com.apple.WebDriver/". You also cannot change the log level, you get whatever Safari gives you.

  • To get a File object with absolute path to the directory: getDirectory()
  • To get a list of all files in that directory; getList()
  • To get the last file in the directory (most likely to be the logs you are looking for): getFile()