Skip to content

Commit

Permalink
[rb] allow default_level to be passed into constructor so client proj…
Browse files Browse the repository at this point in the history
…ects can keep existing behavior
  • Loading branch information
titusfortner committed May 6, 2023
1 parent c4776a3 commit 1cd84f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion rb/lib/selenium/webdriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def self.for(*args)
#

def self.logger(**opts)
@logger ||= WebDriver::Logger.new('Selenium', **opts)
level = $DEBUG || ENV.key?('DEBUG') ? :debug : :info
@logger ||= WebDriver::Logger.new('Selenium', default_level: level, **opts)
end
end # WebDriver
end # Selenium
14 changes: 6 additions & 8 deletions rb/lib/selenium/webdriver/common/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class Logger
#
# @param [String] progname Allow child projects to use Selenium's Logger pattern
#
def initialize(progname = 'Selenium', ignored: nil, allowed: nil)
@logger = create_logger(progname)
def initialize(progname = 'Selenium', default_level: nil, ignored: nil, allowed: nil)
default_level ||= $DEBUG || ENV.key?('DEBUG') ? :debug : :warn

@logger = create_logger(progname, level: default_level)
@ignored = Array(ignored)
@allowed = Array(allowed)
@first_warning = false
Expand Down Expand Up @@ -174,21 +176,17 @@ def deprecate(old, new = nil, id: [], reference: '', &block)

private

def create_logger(name)
def create_logger(name, level:)
logger = ::Logger.new($stdout)
logger.progname = name
logger.level = default_level
logger.level = level
logger.formatter = proc do |severity, time, progname, msg|
"#{time.strftime('%F %T')} #{severity} #{progname} #{msg}\n"
end

logger
end

def default_level
$DEBUG || ENV.key?('DEBUG') ? :debug : :info
end

def discard_or_log(level, message, id)
id = Array(id)
return if (@ignored & id).any?
Expand Down
7 changes: 5 additions & 2 deletions rb/spec/unit/selenium/webdriver/common/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Selenium
module WebDriver
describe Logger do
subject(:logger) { described_class.new('Selenium', ignored: [:logger_info]) }
subject(:logger) { described_class.new('Selenium', default_level: :info, ignored: [:logger_info]) }

around do |example|
debug = $DEBUG
Expand Down Expand Up @@ -53,6 +53,8 @@ module WebDriver

it 'logs at debug level if $DEBUG is set to true' do
$DEBUG = true
logger = described_class.new('Selenium')
$DEBUG = nil
expect(logger.level).to eq(0)
expect(logger).to be_debug
end
Expand Down Expand Up @@ -104,7 +106,8 @@ module WebDriver

describe '#info' do
it 'logs info on first info but not second' do
logger = described_class.new('Selenium')
logger = described_class.new('Selenium', default_level: :info)

expect { logger.info('first') }.to output(/:logger_info/).to_stdout_from_any_process
expect { logger.info('second') }.not_to output(/:logger_info/).to_stdout_from_any_process
end
Expand Down

0 comments on commit 1cd84f7

Please sign in to comment.