diff --git a/rb/lib/selenium/webdriver.rb b/rb/lib/selenium/webdriver.rb index 62901a0f6d724..8240da9273903 100644 --- a/rb/lib/selenium/webdriver.rb +++ b/rb/lib/selenium/webdriver.rb @@ -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 diff --git a/rb/lib/selenium/webdriver/common/logger.rb b/rb/lib/selenium/webdriver/common/logger.rb index 9d0384f841251..cd5cb7b1982b4 100644 --- a/rb/lib/selenium/webdriver/common/logger.rb +++ b/rb/lib/selenium/webdriver/common/logger.rb @@ -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 @@ -174,10 +176,10 @@ 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 @@ -185,10 +187,6 @@ def create_logger(name) 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? diff --git a/rb/spec/unit/selenium/webdriver/common/logger_spec.rb b/rb/spec/unit/selenium/webdriver/common/logger_spec.rb index 46262745ad59e..f65cbd2ea4f50 100644 --- a/rb/spec/unit/selenium/webdriver/common/logger_spec.rb +++ b/rb/spec/unit/selenium/webdriver/common/logger_spec.rb @@ -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 @@ -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 @@ -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