class Selenium::WebDriver::Logger

@example Enable full logging

Selenium::WebDriver.logger.level = :debug

@example Log to file

Selenium::WebDriver.logger.output = 'selenium.log'

@example Use logger manually

Selenium::WebDriver.logger.info('This is info message')
Selenium::WebDriver.logger.warn('This is warning message')

Public Class Methods

new(progname = 'Selenium', ignored: nil) click to toggle source

@param [String] progname Allow child projects to use Selenium’s Logger pattern

# File lib/selenium/webdriver/common/logger.rb, line 51
def initialize(progname = 'Selenium', ignored: nil)
  @logger = create_logger(progname)
  @ignored = Array(ignored)
  @first_warning = false
end

Public Instance Methods

deprecate(old, new = nil, id: [], reference: '', &block) click to toggle source

Marks code as deprecated with/without replacement.

@param [String] old @param [String, nil] new @param [Symbol, Array<Symbol>] id @param [String] reference @yield appends additional message to end of provided template

# File lib/selenium/webdriver/common/logger.rb, line 123
def deprecate(old, new = nil, id: [], reference: '', &block)
  id = Array(id)
  return if @ignored.include?(:deprecations) || (@ignored & id).any?

  ids = id.empty? ? '' : "[#{id.map(&:inspect).join(', ')}] "

  message = +"[DEPRECATION] #{ids}#{old} is deprecated"
  message << if new
               ". Use #{new} instead."
             else
               ' and will be removed in a future release.'
             end
  message << " See explanation for this deprecation: #{reference}." unless reference.empty?

  warn message, &block
end
ignore(id) click to toggle source

Will not log the provided ID.

@param [Array, Symbol] id

# File lib/selenium/webdriver/common/logger.rb, line 86
def ignore(id)
  Array(id).each { |ignore| @ignored << ignore }
end
io() click to toggle source

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

# File lib/selenium/webdriver/common/logger.rb, line 77
def io
  @logger.instance_variable_get(:@logdev).dev
end
output=(io) click to toggle source

Changes logger output to a new IO.

@param [String] io

# File lib/selenium/webdriver/common/logger.rb, line 62
def output=(io)
  @logger.reopen(io)
end
warn(message, id: []) click to toggle source

Overrides default warn to skip ignored messages by provided id

@param [String] message @param [Symbol, Array<Sybmol>] id @yield see deprecate

# File lib/selenium/webdriver/common/logger.rb, line 97
def warn(message, id: [])
  unless @first_warning
    @first_warning = true
    warn("Details on how to use and modify Selenium logger:\n", id: [:logger_info]) do
      "https://selenium.dev/documentation/webdriver/troubleshooting/logging#ruby\n"
    end
  end

  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

Private Instance Methods

create_logger(name) click to toggle source
# File lib/selenium/webdriver/common/logger.rb, line 142
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"
  end

  logger
end
default_level() click to toggle source
# File lib/selenium/webdriver/common/logger.rb, line 153
def default_level
  $DEBUG || ENV.key?('DEBUG') ? :debug : :warn
end