Skip to content

Commit

Permalink
remove custom Logger implementation and use Selenium's new Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Oct 18, 2021
1 parent a9a56b7 commit 008e101
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 94 deletions.
2 changes: 1 addition & 1 deletion lib/webdrivers/common.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require 'rubygems/package'
require 'webdrivers/logger'
require 'webdrivers/network'
require 'webdrivers/system'
require 'selenium-webdriver'
require 'webdrivers/logger'
require 'webdrivers/version'

module Webdrivers
Expand Down
95 changes: 2 additions & 93 deletions lib/webdrivers/logger.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# frozen_string_literal: true

require 'forwardable'
require 'logger'

# Code adapted from Selenium Implementation
# https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/common/logger.rb

module Webdrivers
#
# @example Enable full logging
Expand All @@ -18,94 +12,9 @@ module Webdrivers
# Webdrivers.logger.info('This is info message')
# Webdrivers.logger.warn('This is warning message')
#
class Logger
extend Forwardable
include ::Logger::Severity

def_delegators :@logger, :debug, :debug?,
:info, :info?,
:warn, :warn?,
:error, :error?,
:fatal, :fatal?,
:level

class Logger < Selenium::WebDriver::Logger
def initialize
@logger = create_logger($stdout)
end

def output=(io)
# `Logger#reopen` was added in Ruby 2.3
if @logger.respond_to?(:reopen)
@logger.reopen(io)
else
@logger = create_logger(io)
end
end

#
# For Ruby < 2.3 compatibility
# Based on https://github.com/ruby/ruby/blob/ruby_2_3/lib/logger.rb#L250
#

def level=(severity)
if severity.is_a?(Integer)
@logger.level = severity
else
case severity.to_s.downcase
when 'debug'
@logger.level = DEBUG
when 'info'
@logger.level = INFO
when 'warn'
@logger.level = WARN
when 'error'
@logger.level = ERROR
when 'fatal'
@logger.level = FATAL
when 'unknown'
@logger.level = UNKNOWN
else
raise ArgumentError, "invalid log level: #{severity}"
end
end
end

#
# Returns IO object used by logger internally.
#
# Normally, we would have never needed it, but we want to
# use it as IO object for all child processes to ensure their
# output is redirected there.
#
# It is only used in debug level, in other cases output is suppressed.
#
# @api private
#
def io
@logger.instance_variable_get(:@logdev).instance_variable_get(:@dev)
end

#
# Marks code as deprecated with replacement.
#
# @param [String] old
# @param [String] new
#
def deprecate(old, new)
warn "[DEPRECATION] #{old} is deprecated. Use #{new} instead."
end

private

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

logger
super('Webdrivers')
end
end
end

0 comments on commit 008e101

Please sign in to comment.