Skip to content

Commit c4776a3

Browse files
committed
[rb] allow both allowing and ignoring logging messages by id
1 parent 2d022c0 commit c4776a3

5 files changed

Lines changed: 48 additions & 8 deletions

File tree

rb/.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Metrics/CyclomaticComplexity:
4242
Max: 9
4343
Exclude:
4444
- 'lib/selenium/webdriver/support/color.rb'
45+
- 'lib/selenium/webdriver/common/logger.rb'
4546

4647
Metrics/MethodLength:
4748
CountComments: false
@@ -62,6 +63,7 @@ Metrics/PerceivedComplexity:
6263
Max: 9
6364
Exclude:
6465
- 'lib/selenium/webdriver/common/local_driver.rb'
66+
- 'lib/selenium/webdriver/common/logger.rb'
6567

6668
Naming/FileName:
6769
Exclude:

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ class Logger
4848
#
4949
# @param [String] progname Allow child projects to use Selenium's Logger pattern
5050
#
51-
def initialize(progname = 'Selenium', ignored: nil)
51+
def initialize(progname = 'Selenium', ignored: nil, allowed: nil)
5252
@logger = create_logger(progname)
5353
@ignored = Array(ignored)
54+
@allowed = Array(allowed)
5455
@first_warning = false
5556
end
5657

5758
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+
if level == :info && @logger.level == :info
60+
info(':info is now the default log level, to see additional logging, set log level to :debug')
61+
end
5962

6063
@logger.level = level
6164
end
@@ -87,10 +90,19 @@ def io
8790
#
8891
# Will not log the provided ID.
8992
#
90-
# @param [Array, Symbol] id
93+
# @param [Array, Symbol] ids
94+
#
95+
def ignore(*ids)
96+
@ignored += Array(ids).flatten
97+
end
98+
99+
#
100+
# Will only log the provided ID.
101+
#
102+
# @param [Array, Symbol] ids
91103
#
92-
def ignore(id)
93-
Array(id).each { |ignore| @ignored << ignore }
104+
def allow(ids)
105+
@allowed += Array(ids).flatten
94106
end
95107

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

162+
id << :deprecations if @allowed.include?(:deprecations)
163+
149164
message = +"[DEPRECATION] #{old} is deprecated"
150165
message << if new
151166
". Use #{new} instead."
@@ -177,6 +192,7 @@ def default_level
177192
def discard_or_log(level, message, id)
178193
id = Array(id)
179194
return if (@ignored & id).any?
195+
return if @allowed.any? && (@allowed & id).none?
180196

181197
msg = id.empty? ? message : "[#{id.map(&:inspect).join(', ')}] #{message} "
182198
msg += " #{yield}" if block_given?

rb/spec/unit/selenium/webdriver/chrome/options_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ module Chrome
265265
expect(options.as_json).to eq('browserName' => 'chrome', 'goog:chromeOptions' => {})
266266
end
267267

268-
it 'errors when unrecognized capability is passed' do
268+
it 'warns when unrecognized capability is passed' do
269269
expect {
270270
options.add_option(:foo, 'bar')
271271
}.to have_deprecated(:add_option)

rb/spec/unit/selenium/webdriver/common/logger_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ module WebDriver
175175
end
176176

177177
describe '#ignore' do
178-
it 'prevents logging when ignoring single id' do
178+
it 'prevents logging when id' do
179179
logger.ignore(:foo)
180180
expect { logger.deprecate('#old', '#new', id: :foo) }.not_to output.to_stdout_from_any_process
181181
end
@@ -197,6 +197,28 @@ module WebDriver
197197
expect { logger.deprecate('#old', '#new') }.not_to output.to_stdout_from_any_process
198198
end
199199
end
200+
201+
describe '#allow' do
202+
it 'logs only allowed ids from method' do
203+
logger.allow(:foo)
204+
logger.allow(:bar)
205+
expect { logger.deprecate('#old', '#new', id: :foo) }.to output(/foo/).to_stdout_from_any_process
206+
expect { logger.deprecate('#old', '#new', id: :bar) }.to output(/bar/).to_stdout_from_any_process
207+
expect { logger.deprecate('#old', '#new', id: :foobar) }.not_to output.to_stdout_from_any_process
208+
end
209+
210+
it 'logs only allowed ids from Array' do
211+
logger.allow(%i[foo bar])
212+
expect { logger.deprecate('#old', '#new', id: :foo) }.to output(/foo/).to_stdout_from_any_process
213+
expect { logger.deprecate('#old', '#new', id: :bar) }.to output(/bar/).to_stdout_from_any_process
214+
expect { logger.deprecate('#old', '#new', id: :foobar) }.not_to output.to_stdout_from_any_process
215+
end
216+
217+
it 'prevents logging any deprecation when ignoring :deprecations' do
218+
logger.allow(:deprecations)
219+
expect { logger.deprecate('#old', '#new') }.to output(/new/).to_stdout_from_any_process
220+
end
221+
end
200222
end
201223
end # WebDriver
202224
end # Selenium

rb/spec/unit/selenium/webdriver/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def with_env(hash)
5151
c.define_derived_metadata do |meta|
5252
meta[:aggregate_failures] = true
5353
end
54-
Selenium::WebDriver.logger(ignored: [:logger_info])
54+
Selenium::WebDriver.logger(ignored: :logger_info)
5555

5656
c.include Selenium::WebDriver::UnitSpecHelper
5757

0 commit comments

Comments
 (0)