Class: Watir::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logger::Severity
Defined in:
lib/watir/logger.rb

Overview

Examples:

Enable full logging

Watir.logger.level = :debug

Log to file

Watir.logger.output = 'watir.log'

Use logger manually

Watir.logger.info('This is info message')
Watir.logger.warn('This is warning message')

Instance Method Summary collapse

Constructor Details

#initialize(progname = 'Watir') ⇒ Logger

Returns a new instance of Logger.


30
31
32
33
34
# File 'lib/watir/logger.rb', line 30

def initialize(progname = 'Watir')
  @logger = create_logger($stdout)
  @logger.progname = progname
  @ignored = []
end

Instance Method Details

#deprecate(old, new, reference: '', ids: []) ⇒ Object

Marks code as deprecated with replacement.

Parameters:

  • old (String)
  • new (String)

91
92
93
94
95
96
97
# File 'lib/watir/logger.rb', line 91

def deprecate(old, new, reference: '', ids: [])
  return if @ignored.include?('deprecations') || (@ignored & ids.map!(&:to_s)).any?

  msg = ids.empty? ? '' : "[#{ids.map(&:inspect).join(', ')}] "
  ref_msg = reference.empty? ? '.' : "; see explanation for this deprecation: #{reference}."
  warn "[DEPRECATION] #{msg}#{old} is deprecated. Use #{new} instead#{ref_msg}"
end

#ignore(ids) ⇒ Object


36
37
38
39
# File 'lib/watir/logger.rb', line 36

def ignore(ids)
  ids = [ids] unless ids.is_a? Array
  @ignored.concat ids.map(&:to_s)
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.


81
82
83
# File 'lib/watir/logger.rb', line 81

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

#level=(severity) ⇒ Object

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


59
60
61
62
63
64
65
66
67
68
# File 'lib/watir/logger.rb', line 59

def level=(severity)
  if severity.is_a?(Integer)
    @logger.level = severity
  else
    levels = %w[debug info warn error fatal unknown]
    raise ArgumentError, "invalid log level: #{severity}" unless levels.include? severity.to_s.downcase

    @logger.level = severity.to_s.upcase
  end
end

#output=(io) ⇒ Object


41
42
43
# File 'lib/watir/logger.rb', line 41

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

#warn(message, ids: [], &block) ⇒ Object

Only log a warn message if it is not set to be ignored.


48
49
50
51
52
# File 'lib/watir/logger.rb', line 48

def warn(message, ids: [], &block)
  msg = ids.empty? ? '' : "[#{ids.map!(&:to_s).map(&:inspect).join(', ')}] "
  msg += message
  @logger.warn(msg, &block) unless (@ignored & ids).any?
end