Skip to content

Commit

Permalink
[rb] allow both allowing and ignoring logging messages by id
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed May 6, 2023
1 parent 2d022c0 commit c4776a3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions rb/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Metrics/CyclomaticComplexity:
Max: 9
Exclude:
- 'lib/selenium/webdriver/support/color.rb'
- 'lib/selenium/webdriver/common/logger.rb'

Metrics/MethodLength:
CountComments: false
Expand All @@ -62,6 +63,7 @@ Metrics/PerceivedComplexity:
Max: 9
Exclude:
- 'lib/selenium/webdriver/common/local_driver.rb'
- 'lib/selenium/webdriver/common/logger.rb'

Naming/FileName:
Exclude:
Expand Down
26 changes: 21 additions & 5 deletions rb/lib/selenium/webdriver/common/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ class Logger
#
# @param [String] progname Allow child projects to use Selenium's Logger pattern
#
def initialize(progname = 'Selenium', ignored: nil)
def initialize(progname = 'Selenium', ignored: nil, allowed: nil)
@logger = create_logger(progname)
@ignored = Array(ignored)
@allowed = Array(allowed)
@first_warning = false
end

def level=(level)
info(':info is now the default log level, to see additional logging, set log level to :debug') if level == :info
if level == :info && @logger.level == :info
info(':info is now the default log level, to see additional logging, set log level to :debug')
end

@logger.level = level
end
Expand Down Expand Up @@ -87,10 +90,19 @@ def io
#
# Will not log the provided ID.
#
# @param [Array, Symbol] id
# @param [Array, Symbol] ids
#
def ignore(*ids)
@ignored += Array(ids).flatten
end

#
# Will only log the provided ID.
#
# @param [Array, Symbol] ids
#
def ignore(id)
Array(id).each { |ignore| @ignored << ignore }
def allow(ids)
@allowed += Array(ids).flatten
end

#
Expand Down Expand Up @@ -144,8 +156,11 @@ def warn(message, id: [], &block)
# @yield appends additional message to end of provided template
#
def deprecate(old, new = nil, id: [], reference: '', &block)
id = Array(id)
return if @ignored.include?(:deprecations)

id << :deprecations if @allowed.include?(:deprecations)

message = +"[DEPRECATION] #{old} is deprecated"
message << if new
". Use #{new} instead."
Expand Down Expand Up @@ -177,6 +192,7 @@ def default_level
def discard_or_log(level, message, id)
id = Array(id)
return if (@ignored & id).any?
return if @allowed.any? && (@allowed & id).none?

msg = id.empty? ? message : "[#{id.map(&:inspect).join(', ')}] #{message} "
msg += " #{yield}" if block_given?
Expand Down
2 changes: 1 addition & 1 deletion rb/spec/unit/selenium/webdriver/chrome/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ module Chrome
expect(options.as_json).to eq('browserName' => 'chrome', 'goog:chromeOptions' => {})
end

it 'errors when unrecognized capability is passed' do
it 'warns when unrecognized capability is passed' do
expect {
options.add_option(:foo, 'bar')
}.to have_deprecated(:add_option)
Expand Down
24 changes: 23 additions & 1 deletion rb/spec/unit/selenium/webdriver/common/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ module WebDriver
end

describe '#ignore' do
it 'prevents logging when ignoring single id' do
it 'prevents logging when id' do
logger.ignore(:foo)
expect { logger.deprecate('#old', '#new', id: :foo) }.not_to output.to_stdout_from_any_process
end
Expand All @@ -197,6 +197,28 @@ module WebDriver
expect { logger.deprecate('#old', '#new') }.not_to output.to_stdout_from_any_process
end
end

describe '#allow' do
it 'logs only allowed ids from method' do
logger.allow(:foo)
logger.allow(:bar)
expect { logger.deprecate('#old', '#new', id: :foo) }.to output(/foo/).to_stdout_from_any_process
expect { logger.deprecate('#old', '#new', id: :bar) }.to output(/bar/).to_stdout_from_any_process
expect { logger.deprecate('#old', '#new', id: :foobar) }.not_to output.to_stdout_from_any_process
end

it 'logs only allowed ids from Array' do
logger.allow(%i[foo bar])
expect { logger.deprecate('#old', '#new', id: :foo) }.to output(/foo/).to_stdout_from_any_process
expect { logger.deprecate('#old', '#new', id: :bar) }.to output(/bar/).to_stdout_from_any_process
expect { logger.deprecate('#old', '#new', id: :foobar) }.not_to output.to_stdout_from_any_process
end

it 'prevents logging any deprecation when ignoring :deprecations' do
logger.allow(:deprecations)
expect { logger.deprecate('#old', '#new') }.to output(/new/).to_stdout_from_any_process
end
end
end
end # WebDriver
end # Selenium
2 changes: 1 addition & 1 deletion rb/spec/unit/selenium/webdriver/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def with_env(hash)
c.define_derived_metadata do |meta|
meta[:aggregate_failures] = true
end
Selenium::WebDriver.logger(ignored: [:logger_info])
Selenium::WebDriver.logger(ignored: :logger_info)

c.include Selenium::WebDriver::UnitSpecHelper

Expand Down

0 comments on commit c4776a3

Please sign in to comment.