Class: RightScraper::Loggers::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/right_scraper/loggers/default.rb

Overview

provides a default scraper logger implementation that accumulates errors and warnings but otherwise is a null logger.

Instance Attribute Summary

Attributes inherited from Base

#callback, #errors, #warnings

Instance Method Summary collapse

Methods inherited from Base

#note_phase, #operation

Constructor Details

#initialize(*args) ⇒ Default

Returns a new instance of Default.



33
34
35
36
37
38
39
40
41
42
# File 'lib/right_scraper/loggers/default.rb', line 33

def initialize(*args)
  if args.empty?
    is_windows = !!(RUBY_PLATFORM =~ /mswin|win32|dos|mingw|cygwin/)
    super(is_windows ? 'nul' : '/dev/null')
  else
    super(*args)
  end
  self.level = ::Logger::ERROR
  @recording_messages = true
end

Instance Method Details

#add(severity, message = nil, progname = nil) ⇒ Object

overrides ::Logger#add in order to record errors and warnings logged via the normal logger interface.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/right_scraper/loggers/default.rb', line 63

def add(severity, message = nil, progname = nil)
  if severity >= self.level
    # super logger.
    if message.nil?
      if block_given?
        message = yield
      else
        message = progname
        progname = self.progname
      end
    end
    super(severity, message, progname)

    # record errors (with detail) and warnings.
    if @recording_messages
      if severity >= Logger::ERROR
        @errors << [nil, :log, message]
      elsif severity == ::Logger::WARN
        @warnings << message
      end
    end
  end
  true
end

#note_error(exception, type, explanation = '') ⇒ Object

implements Interface#note_error



45
46
47
48
49
50
51
52
53
# File 'lib/right_scraper/loggers/default.rb', line 45

def note_error(exception, type, explanation = '')
  without_recording_messages do
    explanation = explanation.to_s.strip
    message = "Saw #{exception ? exception.message : 'error'} during #{type}"
    message += ": #{explanation}" unless explanation.empty?
    error(message)
  end
  @errors << [exception, type, explanation]
end

#note_warning(message) ⇒ Object

implements Interface#note_warning



56
57
58
59
# File 'lib/right_scraper/loggers/default.rb', line 56

def note_warning(message)
  without_recording_messages { warn(message) }
  @warnings << message
end