Skip to content

Commit

Permalink
update logging example code and references (#1417)[deploy site]
Browse files Browse the repository at this point in the history
Co-authored-by: Sri Harsha <[email protected]>
  • Loading branch information
titusfortner and harsha509 authored Jun 28, 2023
1 parent b9bc16c commit 8ac6031
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 86 deletions.
5 changes: 0 additions & 5 deletions examples/java/src/test/java/dev/selenium/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
public class BaseTest {
public WebDriver driver;

@BeforeAll
public static void defaultLogging() {
System.setProperty(GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY, DriverService.LOG_NULL);
}

public WebElement getLocatedElement(WebDriver driver, By by) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
return wait.until(d -> driver.findElement(by));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,48 @@
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.manager.SeleniumManager;
import org.openqa.selenium.remote.RemoteWebDriver;

public class LoggingTest {

@Test
public void logging() throws IOException {
Logger logger = Logger.getLogger("");

logger.setLevel(Level.FINE);
Arrays.stream(logger.getHandlers()).forEach(handler -> {
handler.setLevel(Level.FINE);
});

Handler handler = new FileHandler("selenium.xml");
logger.addHandler(handler);

Logger.getLogger(RemoteWebDriver.class.getName()).setLevel(Level.FINEST);
Logger.getLogger(SeleniumManager.class.getName()).setLevel(Level.SEVERE);

Logger localLogger = Logger.getLogger(this.getClass().getName());
localLogger.warning("this is a warning");
localLogger.info("this is useful information");
localLogger.fine("this is detailed debug information");

byte[] bytes = Files.readAllBytes(Paths.get("selenium.xml"));
String fileContent = new String(bytes);

Assertions.assertTrue(fileContent.contains("this is a warning"));
Assertions.assertTrue(fileContent.contains("this is useful information"));
Assertions.assertTrue(fileContent.contains("this is detailed debug information"));
}
@AfterEach
public void loggingOff() {
Logger logger = Logger.getLogger("");
logger.setLevel(Level.INFO);
Arrays.stream(logger.getHandlers()).forEach(handler -> {
handler.setLevel(Level.INFO);
});
}

@Test
public void logging() throws IOException {
Logger logger = Logger.getLogger("");
logger.setLevel(Level.FINE);
Arrays.stream(logger.getHandlers()).forEach(handler -> {
handler.setLevel(Level.FINE);
});

Handler handler = new FileHandler("selenium.xml");
logger.addHandler(handler);

Logger.getLogger(RemoteWebDriver.class.getName()).setLevel(Level.FINEST);
Logger.getLogger(SeleniumManager.class.getName()).setLevel(Level.SEVERE);

Logger localLogger = Logger.getLogger(this.getClass().getName());
localLogger.warning("this is a warning");
localLogger.info("this is useful information");
localLogger.fine("this is detailed debug information");

byte[] bytes = Files.readAllBytes(Paths.get("selenium.xml"));
String fileContent = new String(bytes);

Assertions.assertTrue(fileContent.contains("this is a warning"));
Assertions.assertTrue(fileContent.contains("this is useful information"));
Assertions.assertTrue(fileContent.contains("this is detailed debug information"));
}
}
4 changes: 2 additions & 2 deletions examples/ruby/spec/browsers/chrome_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@
extension_file_path = File.expand_path('../extensions/webextensions-selenium-example.crx', __dir__)
options = Selenium::WebDriver::Options.chrome
options.add_extension(extension_file_path)

@driver = Selenium::WebDriver.for :chrome, options: options
@driver.get("https://www.selenium.dev/selenium/web/blank.html");
@driver.get('https://www.selenium.dev/selenium/web/blank.html')
injected = @driver.find_element(:id, 'webextensions-selenium-example')
expect(injected.text).to eq 'Content injected by webextensions-selenium-example'
end
Expand Down
2 changes: 1 addition & 1 deletion examples/ruby/spec/browsers/firefox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
@driver = Selenium::WebDriver.for :firefox, service: service

profile_location = Dir.new(@driver.capabilities['moz:profile'])
expect(profile_location.path.gsub('\\','/')).to include(root_directory)
expect(profile_location.path.gsub('\\', '/')).to include(root_directory)
end
end

Expand Down
26 changes: 12 additions & 14 deletions examples/ruby/spec/troubleshooting/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
require 'spec_helper'

RSpec.describe 'Logging' do
describe 'Options' do
let(:file_name) { Tempfile.new('logging').path }
let(:file_name) { Tempfile.new('logging').path }

after { FileUtils.rm_f(file_name) }
after { FileUtils.rm_f(file_name) }

it 'logs things' do
logger = Selenium::WebDriver.logger
it 'logs things' do
logger = Selenium::WebDriver.logger

logger.level = :debug
logger.level = :debug

logger.output = file_name
logger.output = file_name

logger.ignore(:jwp_caps, :logger_info)
logger.allow(%i[selenium_manager example_id])
logger.ignore(:jwp_caps, :logger_info)
logger.allow(%i[selenium_manager example_id])

logger.warn('this is a warning', id: :example_id)
logger.info('this is useful information', id: :example_id)
logger.debug('this is detailed debug information', id: :example_id)
logger.warn('this is a warning', id: :example_id)
logger.info('this is useful information', id: :example_id)
logger.debug('this is detailed debug information', id: :example_id)

expect(File.readlines(file_name).grep(/\[:example_id\]/).size).to eq 3
end
expect(File.readlines(file_name).grep(/\[:example_id\]/).size).to eq 3
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Java logs are typically created per class. You can work with the default logger
work with all loggers. To filter out specific classes, see [Filtering](#logger-filtering)

Get the root logger:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L20" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L31" >}}

Java Logging is not exactly straightforward, and if you are just looking for an easy way
to look at the important Selenium logs,
Expand All @@ -41,7 +41,8 @@ For more fine-tuned control, Ruby Selenium created its own Logger class to wrap
This implementation provides some interesting additional features.
Obtain the logger directly from the `#logger`class method on the `Selenium::WebDriver` module:

{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L12" >}}
{{< badge-version version="4.10" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L11" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
```javascript
Expand All @@ -63,7 +64,7 @@ Logger level helps to filter out logs based on their severity.
The default is `INFO`.

You have to change both the level of the logger and the level of the handlers on the root logger:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L22-L25" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L32-L35" >}}
{{% /tab %}}
{{% tab header="Python" %}}
Python has 6 logger levels: `CRITICAL`, `ERROR`, `WARNING`, `INFO`, `DEBUG`, and `NOTSET`.
Expand Down Expand Up @@ -98,10 +99,11 @@ logging.basicConfig(level=logging.WARN)
{{% /tab %}}
{{% tab header="Ruby" %}}
Ruby logger has 5 logger levels: `:debug`, `:info`, `:warn`, `:error`, `:fatal`.
As of Selenium v4.9.1, The default is `:info`.
The default is `:info`.

To change the level of the logger:
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L14" >}}
{{< badge-version version="4.10" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L13" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
JavaScript has 9 logger levels: `OFF`, `SEVERE`, `WARNING`, `INFO`, `DEBUG`, `FINE`, `FINER`, `FINEST`, `ALL`.
Expand Down Expand Up @@ -263,7 +265,7 @@ Logs can be displayed in the console or stored in a file. Different languages ha
{{% tab header="Java" %}}
By default all logs are sent to `System.err`. To direct output to a file, you need to add a handler:

{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L27-L28" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L37-L38" >}}
{{% /tab %}}
{{% tab header="Python" %}}
By default all logs are sent to `sys.stderr`. To direct output somewhere else, you need to add a
Expand All @@ -279,7 +281,8 @@ handler with either a `StreamHandler` or a `FileHandler`:
By default, logs are sent to the console in `stdout`.
To store the logs in a file:

{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L16" >}}
{{< badge-version version="4.10" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L15" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
JavaScript does not currently support sending output to a file.
Expand All @@ -301,7 +304,7 @@ logging.installConsoleHandler()
Java logging is managed on a per class level, so
instead of using the root logger (`Logger.getLogger("")`), set the level you want to use on a per-class
basis:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L30-L31" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L40-L41" >}}
{{% /tab %}}
{{< tab header="Python" >}}
Because logging is managed by module, instead of working with just "selenium", you can specify
Expand All @@ -319,9 +322,11 @@ Everything that Selenium logs includes an ID. You can also turn on or off all de
using `:deprecations`.

These methods accept one or more symbols or an array of symbols:
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#18" >}}
{{< badge-version version="4.10" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#17" >}}
or
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L19" >}}
{{< badge-version version="4.10" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L18" >}}
{{% /tab %}}
{{< tab header="JavaScript" >}}
{{< alert-content >}}{{< /alert-content >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Java logs are typically created per class. You can work with the default logger
work with all loggers. To filter out specific classes, see [Filtering](#logger-filtering)

Get the root logger:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L20" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L31" >}}

Java Logging is not exactly straightforward, and if you are just looking for an easy way
to look at the important Selenium logs,
Expand All @@ -41,7 +41,7 @@ For more fine-tuned control, Ruby Selenium created its own Logger class to wrap
This implementation provides some interesting additional features.
Obtain the logger directly from the `#logger`class method on the `Selenium::WebDriver` module:

{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L12" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L11" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
```javascript
Expand All @@ -63,7 +63,7 @@ Logger level helps to filter out logs based on their severity.
The default is `INFO`.

You have to change both the level of the logger and the level of the handlers on the root logger:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L22-L25" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L32-L35" >}}
{{% /tab %}}
{{% tab header="Python" %}}
Python has 6 logger levels: `CRITICAL`, `ERROR`, `WARNING`, `INFO`, `DEBUG`, and `NOTSET`.
Expand Down Expand Up @@ -101,7 +101,7 @@ logging.basicConfig(level=logging.WARN)
As of Selenium v4.9.1, The default is `:info`.

To change the level of the logger:
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L14" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L13" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
JavaScript has 9 logger levels: `OFF`, `SEVERE`, `WARNING`, `INFO`, `DEBUG`, `FINE`, `FINER`, `FINEST`, `ALL`.
Expand Down Expand Up @@ -263,7 +263,7 @@ Logs can be displayed in the console or stored in a file. Different languages ha
{{% tab header="Java" %}}
By default all logs are sent to `System.err`. To direct output to a file, you need to add a handler:

{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L27-L28" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L37-L38" >}}
{{% /tab %}}
{{% tab header="Python" %}}
By default all logs are sent to `sys.stderr`. To direct output somewhere else, you need to add a
Expand All @@ -279,7 +279,7 @@ handler with either a `StreamHandler` or a `FileHandler`:
By default, logs are sent to the console in `stdout`.
To store the logs in a file:

{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L16" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L15" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
JavaScript does not currently support sending output to a file.
Expand All @@ -301,7 +301,7 @@ logging.installConsoleHandler()
Java logging is managed on a per class level, so
instead of using the root logger (`Logger.getLogger("")`), set the level you want to use on a per-class
basis:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L30-L31" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L40-L41" >}}
{{% /tab %}}
{{< tab header="Python" >}}
Because logging is managed by module, instead of working with just "selenium", you can specify
Expand All @@ -319,9 +319,9 @@ Everything that Selenium logs includes an ID. You can also turn on or off all de
using `:deprecations`.

These methods accept one or more symbols or an array of symbols:
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L18" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L17" >}}
or
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L19" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L18" >}}
{{% /tab %}}
{{< tab header="JavaScript" >}}
{{< alert-content >}}{{< /alert-content >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Java logs are typically created per class. You can work with the default logger
work with all loggers. To filter out specific classes, see [Filtering](#logger-filtering)

Get the root logger:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L20" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L31" >}}

Java Logging is not exactly straightforward, and if you are just looking for an easy way
to look at the important Selenium logs,
Expand All @@ -41,7 +41,7 @@ For more fine-tuned control, Ruby Selenium created its own Logger class to wrap
This implementation provides some interesting additional features.
Obtain the logger directly from the `#logger`class method on the `Selenium::WebDriver` module:

{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L12" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L11" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
```javascript
Expand All @@ -63,7 +63,7 @@ Logger level helps to filter out logs based on their severity.
The default is `INFO`.

You have to change both the level of the logger and the level of the handlers on the root logger:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L22-L25" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L32-L35" >}}
{{% /tab %}}
{{% tab header="Python" %}}
Python has 6 logger levels: `CRITICAL`, `ERROR`, `WARNING`, `INFO`, `DEBUG`, and `NOTSET`.
Expand Down Expand Up @@ -101,7 +101,7 @@ logging.basicConfig(level=logging.WARN)
As of Selenium v4.9.1, The default is `:info`.

To change the level of the logger:
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L14" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L13" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
JavaScript has 9 logger levels: `OFF`, `SEVERE`, `WARNING`, `INFO`, `DEBUG`, `FINE`, `FINER`, `FINEST`, `ALL`.
Expand Down Expand Up @@ -263,7 +263,7 @@ Logs can be displayed in the console or stored in a file. Different languages ha
{{% tab header="Java" %}}
By default all logs are sent to `System.err`. To direct output to a file, you need to add a handler:

{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L27-L28" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L37-L38" >}}
{{% /tab %}}
{{% tab header="Python" %}}
By default all logs are sent to `sys.stderr`. To direct output somewhere else, you need to add a
Expand All @@ -279,7 +279,7 @@ handler with either a `StreamHandler` or a `FileHandler`:
By default, logs are sent to the console in `stdout`.
To store the logs in a file:

{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L16" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L15" >}}
{{% /tab %}}
{{% tab header="JavaScript" %}}
JavaScript does not currently support sending output to a file.
Expand All @@ -301,7 +301,7 @@ logging.installConsoleHandler()
Java logging is managed on a per class level, so
instead of using the root logger (`Logger.getLogger("")`), set the level you want to use on a per-class
basis:
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L30-L31" >}}
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L40-L41" >}}
{{% /tab %}}
{{< tab header="Python" >}}
Because logging is managed by module, instead of working with just "selenium", you can specify
Expand All @@ -319,9 +319,9 @@ Everything that Selenium logs includes an ID. You can also turn on or off all de
using `:deprecations`.

These methods accept one or more symbols or an array of symbols:
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L18" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L17" >}}
or
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L19" >}}
{{< gh-codeblock path="/examples/ruby/spec/troubleshooting/logging_spec.rb#L18" >}}
{{% /tab %}}
{{< tab header="JavaScript" >}}
{{< alert-content >}}{{< /alert-content >}}
Expand Down
Loading

0 comments on commit 8ac6031

Please sign in to comment.