Skip to content

Commit

Permalink
[rb] add features to Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Nov 29, 2019
1 parent 4c568fe commit f5d2163
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 42 deletions.
57 changes: 43 additions & 14 deletions rb/lib/selenium/webdriver/common/logger.rb
Expand Up @@ -45,8 +45,12 @@ class Logger
:fatal, :fatal?,
:level, :level=

def initialize
@logger = create_logger($stdout)
#
# @param [String] progname Allow child projects to use Selenium's Logger pattern
#
def initialize(progname = 'Selenium')
@logger = create_logger(progname)
@ignored = []
end

#
Expand All @@ -73,28 +77,57 @@ def io
@logger.instance_variable_get(:@logdev).dev
end

#
# Will not log the provided ID.
#
# @param [Array, Symbol] id
#
def ignore(id)
Array(id).each { |ignore| @ignored << ignore }
end

#
# Overrides default #warn to skip ignored messages by provided id
#
# @param [String] message
# @param [Symbol, Array<Sybmol>] id
# @yield see #deprecate
#
def warn(message, id: [])
id = Array(id)
return if (@ignored & id).any?

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

@logger.warn { msg }
end

#
# Marks code as deprecated with/without replacement.
#
# @param [String] old
# @param [String, nil] new
# @param [Symbol, Array<Sybmol>] id
# @yield appends additional message to end of provided template
#
def deprecate(old, new = nil)
def deprecate(old, new = nil, id: [], &block)
return if @ignored.include?(:deprecations) || (@ignored & Array(id)).any?

message = +"[DEPRECATION] #{old} is deprecated"
message << if new
". Use #{new} instead."
else
' and will be removed in the next releases.'
' and will be removed in a future release.'
end

warn message
warn message, id: id, &block
end

private

def create_logger(output)
logger = ::Logger.new(output)
logger.progname = 'Selenium'
def create_logger(name)
logger = ::Logger.new($stdout)
logger.progname = name
logger.level = default_level
logger.formatter = proc do |severity, time, progname, msg|
"#{time.strftime('%F %T')} #{severity} #{progname} #{msg}\n"
Expand All @@ -104,11 +137,7 @@ def create_logger(output)
end

def default_level
if $DEBUG || ENV.key?('DEBUG')
:debug
else
:warn
end
$DEBUG || ENV.key?('DEBUG') ? :debug : :warn
end
end # Logger
end # WebDriver
Expand Down
122 changes: 94 additions & 28 deletions rb/spec/unit/selenium/webdriver/common/logger_spec.rb
Expand Up @@ -22,6 +22,8 @@
module Selenium
module WebDriver
describe Logger do
subject(:logger) { Logger.new('Selenium') }

around do |example|
debug = $DEBUG
$DEBUG = false
Expand All @@ -30,46 +32,110 @@ module WebDriver
WebDriver.instance_variable_set(:@logger, nil) # reset cache
end

it 'logs warnings by default' do
expect(WebDriver.logger.level).to eq(2)
expect(WebDriver.logger).to be_warn
describe '#new' do
it 'allows creating a logger with a different progname' do
other_logger = Logger.new('NotSelenium')
msg = /WARN NotSelenium message/
expect { other_logger.warn('message') }.to output(msg).to_stdout_from_any_process
end
end

it 'logs everything if $DEBUG is set to true' do
$DEBUG = true
expect(WebDriver.logger.level).to eq(0)
expect(WebDriver.logger).to be_debug
end
describe '#level' do
it 'logs at warning level by default' do
expect(logger.level).to eq(2)
expect(logger).to be_warn
end

it 'logs at debug level if $DEBUG is set to true' do
$DEBUG = true
expect(logger.level).to eq(0)
expect(logger).to be_debug
end

it 'allows changing level by name during execution' do
logger.level = :info
expect(logger.level).to eq(1)
expect(logger).to be_info
end

it 'allows to change level during execution' do
WebDriver.logger.level = :info
expect(WebDriver.logger.level).to eq(1)
expect(WebDriver.logger).to be_info
it 'allows changing level by integer during execution' do
logger.level = 1
expect(logger).to be_info
end
end

it 'outputs to stdout by default' do
expect { WebDriver.logger.warn('message') }.to output(/WARN Selenium message/).to_stdout
describe '#output' do
it 'outputs to stdout by default' do
expect { logger.warn('message') }.to output(/WARN Selenium message/).to_stdout
end

it 'allows output to file' do
begin
logger.output = 'test.log'
logger.warn('message')
expect(File.read('test.log')).to include('WARN Selenium message')
ensure
logger.close
File.delete('test.log')
end
end
end

it 'allows to output to file' do
begin
WebDriver.logger.output = 'test.log'
WebDriver.logger.warn('message')
expect(File.read('test.log')).to include('WARN Selenium message')
ensure
WebDriver.logger.close
File.delete('test.log')
describe '#warn' do
it 'logs with String' do
expect { logger.warn "String Value" }.to output(/WARN Selenium String Value/).to_stdout
end

it 'logs single id when set' do
msg = /WARN Selenium \[:foo\] warning message/
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stdout
end

it 'logs multiple ids when set' do
msg = /WARN Selenium \[:foo, :bar\] warning message/
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stdout
end
end

it 'allows to deprecate functionality with replacement' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\./
expect { WebDriver.logger.deprecate('#old', '#new') }.to output(message).to_stdout
describe '#deprecate' do
it 'allows to deprecate functionality with replacement' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\./
expect { logger.deprecate('#old', '#new') }.to output(message).to_stdout
end

it 'allows to deprecate functionality without replacement' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated and will be removed in a future release\./
expect { logger.deprecate('#old') }.to output(message).to_stdout
end

it 'appends deprecation message with provided block' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\. More Details\./
expect { logger.deprecate('#old', '#new') { 'More Details.' } }.to output(message).to_stdout
end
end

it 'allows to deprecate functionality without replacement' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated and will be removed in the next releases\./
expect { WebDriver.logger.deprecate('#old') }.to output(message).to_stdout
describe '#ignore' do
it 'prevents logging when ignoring single id' do
logger.ignore(:foo)
expect { logger.deprecate('#old', '#new', id: :foo) }.not_to output.to_stdout_from_any_process
end

it 'prevents logging when ignoring multiple ids' do
logger.ignore(:foo)
logger.ignore(:bar)
expect { logger.deprecate('#old', '#new', id: :foo) }.not_to output.to_stdout_from_any_process
expect { logger.deprecate('#old', '#new', id: :bar) }.not_to output.to_stdout_from_any_process
end

it 'prevents logging when ignoring Array of ids' do
logger.ignore(%i[foo bar])
expect { logger.deprecate('#old', '#new', id: %i[foo foobar]) }.not_to output.to_stdout_from_any_process
end

it 'prevents logging any deprecation when ignoring :deprecations' do
logger.ignore(:deprecations)
expect { logger.deprecate('#old', '#new') }.not_to output.to_stdout_from_any_process
end
end
end
end # WebDriver
Expand Down

0 comments on commit f5d2163

Please sign in to comment.