Skip to content

Commit 2d022c0

Browse files
committed
[rb] change logging default to :info and support ignoring any logging
1 parent 156e7f5 commit 2d022c0

25 files changed

+189
-108
lines changed

rb/lib/selenium/devtools.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class << self
2525
def load_version
2626
require "selenium/devtools/v#{@version}"
2727
rescue LoadError
28-
Kernel.warn "Could not load selenium-devtools v#{@version}. Trying older versions."
28+
WebDriver.logger.warn "Could not load selenium-devtools v#{@version}. Trying older versions.",
29+
id: :devtools
2930
load_older_version
3031
end
3132

@@ -35,13 +36,19 @@ def load_version
3536
def load_older_version
3637
load_old_version(@version - 1)
3738
rescue LoadError
38-
load_old_version(@version - 2)
39+
begin
40+
load_old_version(@version - 2)
41+
rescue LoadError
42+
raise WebDriver::Error::WebDriverError,
43+
'Could not find a valid devtools version; use a more recent version of selenium-devtools gem'
44+
end
3945
end
4046

4147
def load_old_version(version)
4248
require "selenium/devtools/v#{version}"
4349
self.version = version
44-
Kernel.warn "Using selenium-devtools version v#{version}, some features may not work as expected."
50+
msg = "Using selenium-devtools version v#{version}, some features may not work as expected."
51+
WebDriver.logger.warn msg, id: :devtools
4552
end
4653
end
4754
end # DevTools

rb/lib/selenium/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def process
254254
args = ['-jar', @jar, @role, '--port', @port.to_s]
255255
server_command = ['java'] + properties + args + @additional_args
256256
cp = WebDriver::ChildProcess.build(*server_command)
257-
WebDriver.logger.debug("Executing Process #{server_command}")
257+
WebDriver.logger.debug("Executing Process #{server_command}", id: :server)
258258

259259
if @log.is_a?(String)
260260
cp.io = @log

rb/lib/selenium/webdriver/common/action_builder.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,6 @@ def add_input(device)
250250
@devices << device
251251
device
252252
end
253-
254-
def deprecate_method(device = nil, duration = nil, number = nil, method: :pause)
255-
return unless device || number || duration
256-
257-
WebDriver.logger.deprecate "ActionBuilder##{method} with ordered parameters",
258-
':device, :duration, :number keywords',
259-
id: method
260-
end
261253
end # ActionBuilder
262254
end # WebDriver
263255
end # Selenium

rb/lib/selenium/webdriver/common/child_process.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def start
5353
options = {%i[out err] => io}
5454
options[:pgroup] = true unless Platform.windows? # NOTE: this is a bug only in Windows 7
5555

56-
WebDriver.logger.debug("Starting process: #{@command} with #{options}")
56+
WebDriver.logger.debug("Starting process: #{@command} with #{options}", id: :process)
5757
@pid = Process.spawn(*@command, options)
58-
WebDriver.logger.debug(" -> pid: #{@pid}")
58+
WebDriver.logger.debug(" -> pid: #{@pid}", id: :process)
5959

6060
Process.detach(@pid) if detach
6161
end
@@ -64,16 +64,16 @@ def stop(timeout = 3)
6464
return unless @pid
6565
return if exited?
6666

67-
WebDriver.logger.debug("Sending TERM to process: #{@pid}")
67+
WebDriver.logger.debug("Sending TERM to process: #{@pid}", id: :process)
6868
terminate(@pid)
6969
poll_for_exit(timeout)
7070

71-
WebDriver.logger.debug(" -> stopped #{@pid}")
71+
WebDriver.logger.debug(" -> stopped #{@pid}", id: :process)
7272
rescue TimeoutError, Errno::EINVAL
73-
WebDriver.logger.debug(" -> sending KILL to process: #{@pid}")
73+
WebDriver.logger.debug(" -> sending KILL to process: #{@pid}", id: :process)
7474
kill(@pid)
7575
wait
76-
WebDriver.logger.debug(" -> killed #{@pid}")
76+
WebDriver.logger.debug(" -> killed #{@pid}", id: :process)
7777
end
7878

7979
def alive?
@@ -83,18 +83,18 @@ def alive?
8383
def exited?
8484
return unless @pid
8585

86-
WebDriver.logger.debug("Checking if #{@pid} is exited:")
86+
WebDriver.logger.debug("Checking if #{@pid} is exited:", id: :process)
8787
_, @status = Process.waitpid2(@pid, Process::WNOHANG | Process::WUNTRACED) if @status.nil?
8888
return if @status.nil?
8989

9090
exit_code = @status.exitstatus || @status.termsig
91-
WebDriver.logger.debug(" -> exit code is #{exit_code.inspect}")
91+
WebDriver.logger.debug(" -> exit code is #{exit_code.inspect}", id: :process)
9292

9393
!!exit_code
9494
end
9595

9696
def poll_for_exit(timeout)
97-
WebDriver.logger.debug("Polling #{timeout} seconds for exit of #{@pid}")
97+
WebDriver.logger.debug("Polling #{timeout} seconds for exit of #{@pid}", id: :process)
9898

9999
end_time = Time.now + timeout
100100
sleep POLL_INTERVAL until exited? || Time.now > end_time

rb/lib/selenium/webdriver/common/driver_finder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def self.path(options, klass)
2828
path ||= begin
2929
SeleniumManager.driver_path(options)
3030
rescue StandardError => e
31-
WebDriver.logger.warn("Unable obtain driver using Selenium Manager\n #{e.message}")
31+
WebDriver.logger.warn("Unable to obtain driver using Selenium Manager\n #{e.message}",
32+
id: :selenium_manager)
3233
nil
3334
end
3435
msg = "Unable to locate the #{klass::EXECUTABLE} executable; for more information on how to install drivers, " \

rb/lib/selenium/webdriver/common/logger.rb

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class Logger
3838

3939
def_delegators :@logger,
4040
:close,
41-
:debug, :debug?,
42-
:info, :info?,
41+
:debug?,
42+
:info?,
4343
:warn?,
4444
:error, :error?,
4545
:fatal, :fatal?,
46-
:level, :level=
46+
:level
4747

4848
#
4949
# @param [String] progname Allow child projects to use Selenium's Logger pattern
@@ -54,6 +54,12 @@ def initialize(progname = 'Selenium', ignored: nil)
5454
@first_warning = false
5555
end
5656

57+
def level=(level)
58+
info(':info is now the default log level, to see additional logging, set log level to :debug') if level == :info
59+
60+
@logger.level = level
61+
end
62+
5763
#
5864
# Changes logger output to a new IO.
5965
#
@@ -88,27 +94,44 @@ def ignore(id)
8894
end
8995

9096
#
91-
# Overrides default #warn to skip ignored messages by provided id
97+
# Used to supply information of interest for debugging a problem
98+
# Overrides default #debug to skip ignored messages by provided id
9299
#
93100
# @param [String] message
94101
# @param [Symbol, Array<Sybmol>] id
95102
# @yield see #deprecate
96103
#
97-
def warn(message, id: [])
104+
def debug(message, id: [], &block)
105+
discard_or_log(:debug, message, id, &block)
106+
end
107+
108+
#
109+
# Used to supply information of general interest
110+
#
111+
# @param [String] message
112+
# @param [Symbol, Array<Sybmol>] id
113+
# @yield see #deprecate
114+
#
115+
def info(message, id: [], &block)
98116
unless @first_warning
99117
@first_warning = true
100-
warn("Details on how to use and modify Selenium logger:\n", id: [:logger_info]) do
101-
"https://selenium.dev/documentation/webdriver/troubleshooting/logging#ruby\n"
118+
info("Details on how to use and modify Selenium logger:\n", id: [:logger_info]) do
119+
"https://selenium.dev/documentation/webdriver/troubleshooting/logging\n"
102120
end
103121
end
104122

105-
id = Array(id)
106-
return if (@ignored & id).any?
107-
108-
msg = id.empty? ? message : "[#{id.map(&:inspect).join(', ')}] #{message} "
109-
msg += " #{yield}" if block_given?
123+
discard_or_log(:info, message, id, &block)
124+
end
110125

111-
@logger.warn { msg }
126+
#
127+
# Used to supply information that suggests action be taken by user
128+
#
129+
# @param [String] message
130+
# @param [Symbol, Array<Sybmol>] id
131+
# @yield see #deprecate
132+
#
133+
def warn(message, id: [], &block)
134+
discard_or_log(:warn, message, id, &block)
112135
end
113136

114137
#
@@ -121,20 +144,17 @@ def warn(message, id: [])
121144
# @yield appends additional message to end of provided template
122145
#
123146
def deprecate(old, new = nil, id: [], reference: '', &block)
124-
id = Array(id)
125-
return if @ignored.include?(:deprecations) || (@ignored & id).any?
126-
127-
ids = id.empty? ? '' : "[#{id.map(&:inspect).join(', ')}] "
147+
return if @ignored.include?(:deprecations)
128148

129-
message = +"[DEPRECATION] #{ids}#{old} is deprecated"
149+
message = +"[DEPRECATION] #{old} is deprecated"
130150
message << if new
131151
". Use #{new} instead."
132152
else
133153
' and will be removed in a future release.'
134154
end
135155
message << " See explanation for this deprecation: #{reference}." unless reference.empty?
136156

137-
warn message, &block
157+
discard_or_log(:warn, message, id, &block)
138158
end
139159

140160
private
@@ -151,7 +171,17 @@ def create_logger(name)
151171
end
152172

153173
def default_level
154-
$DEBUG || ENV.key?('DEBUG') ? :debug : :warn
174+
$DEBUG || ENV.key?('DEBUG') ? :debug : :info
175+
end
176+
177+
def discard_or_log(level, message, id)
178+
id = Array(id)
179+
return if (@ignored & id).any?
180+
181+
msg = id.empty? ? message : "[#{id.map(&:inspect).join(', ')}] #{message} "
182+
msg += " #{yield}" if block_given?
183+
184+
@logger.send(level) { msg }
155185
end
156186
end # Logger
157187
end # WebDriver

rb/lib/selenium/webdriver/common/options.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def as_json(*)
128128

129129
unless options.empty?
130130
msg = 'These options are not w3c compliant and will result in failures in a future release'
131-
WebDriver.logger.warn("#{msg}: #{options}")
131+
WebDriver.logger.warn("#{msg}: #{options}", id: :w3c_options)
132132
browser_options.merge!(options)
133133
end
134134

rb/lib/selenium/webdriver/common/port_prober.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def self.free?(port)
3434
Platform.interfaces.each do |host|
3535
TCPServer.new(host, port).close
3636
rescue *IGNORED_ERRORS => e
37-
WebDriver.logger.debug("port prober could not bind to #{host}:#{port} (#{e.message})")
37+
WebDriver.logger.debug("port prober could not bind to #{host}:#{port} (#{e.message})", id: :driver_service)
3838
# ignored - some machines appear unable to bind to some of their interfaces
3939
end
4040

rb/lib/selenium/webdriver/common/selenium_manager.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class << self
3333
# @param [Options] options browser options.
3434
# @return [String] the path to the correct driver.
3535
def driver_path(options)
36-
message = 'applicable driver not found; attempting to install with Selenium Manager'
37-
WebDriver.logger.warn(message)
36+
message = 'applicable driver not found; attempting to install with Selenium Manager (Beta)'
37+
WebDriver.logger.info(message, id: :selenium_manager)
3838

3939
unless options.is_a?(Options)
4040
raise ArgumentError, "SeleniumManager requires a WebDriver::Options instance, not #{options.inspect}"
@@ -52,7 +52,7 @@ def driver_path(options)
5252
command << '--debug' if WebDriver.logger.debug?
5353

5454
location = run(*command)
55-
WebDriver.logger.debug("Driver found at #{location}")
55+
WebDriver.logger.debug("Driver found at #{location}", id: :selenium_manager)
5656
Platform.assert_executable location
5757

5858
location
@@ -76,13 +76,13 @@ def binary
7676
raise Error::WebDriverError, 'Unable to obtain Selenium Manager'
7777
end
7878

79-
WebDriver.logger.debug("Selenium Manager found at #{location}")
79+
WebDriver.logger.debug("Selenium Manager found at #{location}", id: :selenium_manager)
8080
location
8181
end
8282
end
8383

8484
def run(*command)
85-
WebDriver.logger.debug("Executing Process #{command}")
85+
WebDriver.logger.debug("Executing Process #{command}", id: :selenium_manager)
8686

8787
begin
8888
stdout, stderr, status = Open3.capture3(*command)
@@ -97,7 +97,7 @@ def run(*command)
9797
end
9898

9999
json_output['logs'].each do |log|
100-
WebDriver.logger.send(log['level'].downcase, log['message'])
100+
WebDriver.logger.send(log['level'].downcase, log['message'], id: :selenium_manager)
101101
end
102102

103103
result

rb/lib/selenium/webdriver/common/service_manager.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def uri
7878
private
7979

8080
def build_process(*command)
81-
WebDriver.logger.debug("Executing Process #{command}")
81+
WebDriver.logger.debug("Executing Process #{command}", id: :driver_service)
8282
@process = ChildProcess.build(*command)
8383
@process.io = @io
8484
@process.io ||= WebDriver.logger.io if WebDriver.logger.debug?

0 commit comments

Comments
 (0)