Class: Selenium::WebDriver::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/selenium/webdriver/common/logger.rb

Overview

Examples:

Enable full logging

Selenium::WebDriver.logger.level = :debug

Log to file

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

Use logger manually

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

Instance Method Summary collapse

Constructor Details

#initialize(progname = 'Selenium', ignored: nil) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • progname (String) (defaults to: 'Selenium')

    Allow child projects to use Selenium’s Logger pattern



51
52
53
54
55
# 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

Instance Method Details

#deprecate(old, new = nil, id: [], reference: '') { ... } ⇒ Object

Marks code as deprecated with/without replacement.

Parameters:

  • old (String)
  • new (String, nil) (defaults to: nil)
  • id (Symbol, Array<Symbol>) (defaults to: [])
  • reference (String) (defaults to: '')

Yields:

  • appends additional message to end of provided template



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Object

Will not log the provided ID.

Parameters:

  • id (Array, Symbol)


86
87
88
# File 'lib/selenium/webdriver/common/logger.rb', line 86

def ignore(id)
  Array(id).each { |ignore| @ignored << ignore }
end

#ioObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



77
78
79
# File 'lib/selenium/webdriver/common/logger.rb', line 77

def io
  @logger.instance_variable_get(:@logdev).dev
end

#output=(io) ⇒ Object

Changes logger output to a new IO.

Parameters:

  • io (String)


62
63
64
# File 'lib/selenium/webdriver/common/logger.rb', line 62

def output=(io)
  @logger.reopen(io)
end

#warn(message, id: []) { ... } ⇒ Object

Overrides default #warn to skip ignored messages by provided id

Parameters:

  • message (String)
  • id (Symbol, Array<Sybmol>) (defaults to: [])

Yields:

  • see #deprecate



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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