Class: Hubbado::Log::Controls::LogHandler

Inherits:
LogHandler
  • Object
show all
Defined in:
lib/hubbado/log/controls/log_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LogHandler

#traces?

Class Method Details

.attach(instance) ⇒ Object

Replaces a class's logger with one writing here, keeping its subject. This handler records rather than displays, so the operator's settings do not decide what a spec attaching one can read: it is asking what the class said, and the process's filters are not its subject.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hubbado/log/controls/log_handler.rb', line 15

def self.attach(instance)
  logger = instance.logger

  if logger.nil?
    raise ArgumentError, "#{instance.class} carries no logger to attach to. " \
                         "Use .logger for a class that is handed one instead."
  end

  new.tap do |handler|
    instance.logger = Log::Logger.new(logger.subject, [handler])
  end
end

.logger(subject = nil) ⇒ Object

For a class handed a logger rather than carrying one, and for a spec that wants both.



29
30
31
32
33
34
# File 'lib/hubbado/log/controls/log_handler.rb', line 29

def self.logger(subject = nil)
  subject ||= Subject.example
  handler = new

  [handler, Log::Logger.new(subject, [handler])]
end

Instance Method Details

#log(subject, severity, message, data = nil, stacktrace = nil, tags = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hubbado/log/controls/log_handler.rb', line 36

def log(subject, severity, message, data = nil, stacktrace = nil, tags = nil)
  tags ||= []

  messages << {
    subject: subject,
    severity: severity,
    message: message,
    data: data,
    stacktrace: stacktrace,
    tags: tags
  }
end

#logged?(severity = nil) ⇒ Boolean

Named without a severity, this answers whether anything was written at all — which a spec would otherwise have to infer from an attribute never having been set. A severity is compared as a symbol, because #log takes a String as readily and passes on what it was given.

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/hubbado/log/controls/log_handler.rb', line 53

def logged?(severity = nil)
  return !messages.empty? if severity.nil?

  messages.any? { |written| written.fetch(:severity).to_s.to_sym == severity.to_s.to_sym }
end

#messagesObject

Everything a class logged, in order, because a run that reports two failures needs both of them. The attributes below read the most recent.



7
8
9
# File 'lib/hubbado/log/controls/log_handler.rb', line 7

def messages
  @messages ||= []
end

#resetObject



59
60
61
# File 'lib/hubbado/log/controls/log_handler.rb', line 59

def reset
  @messages = []
end