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

Class Method Details

.attach(instance, level: :trace, tags: Tags::ALL) ⇒ Object

Replaces a class's logger with one writing here, keeping its subject. Neither the configured level nor the configured tags decide what a spec attaching one of these can read: it is asking what the class said, and the process's filters are not its subject.



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

def self.attach(instance, level: :trace, tags: Tags::ALL)
  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 = Logger.new(logger.subject, [handler], level: level, tags: tags)
  end
end

.logger(subject = Subject.example, level: :trace, tags: Tags::ALL) ⇒ Object

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



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

def self.logger(subject = Subject.example, level: :trace, tags: Tags::ALL)
  handler = new

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

Instance Method Details

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



34
35
36
37
38
39
40
41
42
# File 'lib/hubbado/log/controls/log_handler.rb', line 34

def log(subject, severity, message, data = nil, stacktrace = nil)
  messages << {
    subject: subject,
    severity: severity,
    message: message,
    data: data,
    stacktrace: stacktrace
  }
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)


48
49
50
51
52
# File 'lib/hubbado/log/controls/log_handler.rb', line 48

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



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

def reset
  @messages = []
end