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', default_level: nil, ignored: nil, allowed: 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
56
57
58
59
60
# File 'lib/selenium/webdriver/common/logger.rb', line 51

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
  @level_forced = false
  @output_forced = false
end

Instance Method Details

#allow(*ids) ⇒ Object

Will only log the provided ID.

Parameters:

  • ids (Array, Symbol)


134
135
136
# File 'lib/selenium/webdriver/common/logger.rb', line 134

def allow(*ids)
  @allowed += Array(ids).flatten
end

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

Used to supply information of interest for debugging a problem Overrides default #debug to skip ignored messages by provided id

Parameters:

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

Yields:

  • see #deprecate



146
147
148
# File 'lib/selenium/webdriver/common/logger.rb', line 146

def debug(message, id: [], &block)
  discard_or_log(:debug, message, id, &block)
end

#debug!Object

Forces debug level and prevents it from being overridden.



65
66
67
68
# File 'lib/selenium/webdriver/common/logger.rb', line 65

def debug!
  @level_forced = true
  @logger.level = :debug
end

#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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/selenium/webdriver/common/logger.rb', line 192

def deprecate(old, new = nil, id: [], reference: '', &block)
  id = Array(id)
  return if @ignored.include?(:deprecations)

  id << :deprecations if @allowed.include?(:deprecations)

  message = "[DEPRECATION] #{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?

  discard_or_log(:warn, message, id, &block)
end

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

Used to supply information that suggests an error occurred

Parameters:

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

Yields:

  • see #deprecate



168
169
170
# File 'lib/selenium/webdriver/common/logger.rb', line 168

def error(message, id: [], &block)
  discard_or_log(:error, message, id, &block)
end

#ignore(*ids) ⇒ Object

Will not log the provided ID.

Parameters:

  • ids (Array, Symbol)


125
126
127
# File 'lib/selenium/webdriver/common/logger.rb', line 125

def ignore(*ids)
  @ignored += Array(ids).flatten
end

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

Used to supply information of general interest

Parameters:

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

Yields:

  • see #deprecate



157
158
159
# File 'lib/selenium/webdriver/common/logger.rb', line 157

def info(message, id: [], &block)
  discard_or_log(:info, message, id, &block)
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.



116
117
118
# File 'lib/selenium/webdriver/common/logger.rb', line 116

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

#level=(level) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/selenium/webdriver/common/logger.rb', line 78

def level=(level)
  if @level_forced
    warn('Logger level is forced; ignoring override', id: :logger)
    return
  end

  if level == :info && @logger.level == :info
    info(':info is now the default log level, to see additional logging, set log level to :debug')
  end

  @logger.level = level
end

#output=(io) ⇒ Object

Changes logger output to a new IO.

Parameters:

  • io (String)


96
97
98
99
100
101
102
103
# File 'lib/selenium/webdriver/common/logger.rb', line 96

def output=(io)
  if @output_forced
    warn('Logger output is forced; ignoring override', id: :logger)
    return
  end

  @logger.reopen(io)
end

#stderr!Object

Forces output to stderr and prevents it from being overridden.



73
74
75
76
# File 'lib/selenium/webdriver/common/logger.rb', line 73

def stderr!
  @output_forced = true
  @logger.reopen($stderr)
end

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

Used to supply information that suggests action be taken by user

Parameters:

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

Yields:

  • see #deprecate



179
180
181
# File 'lib/selenium/webdriver/common/logger.rb', line 179

def warn(message, id: [], &block)
  discard_or_log(:warn, message, id, &block)
end