Class: Hubbado::Log::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/hubbado/log/logger.rb,
lib/hubbado/log/logger/substitute.rb

Defined Under Namespace

Modules: Substitute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, log_handlers = nil) ⇒ Logger

Returns a new instance of Logger.



7
8
9
10
# File 'lib/hubbado/log/logger.rb', line 7

def initialize(subject, log_handlers = nil)
  self.subject = subject
  self.log_handlers = Array(log_handlers)
end

Instance Attribute Details

#log_handlersObject

Returns the value of attribute log_handlers.



4
5
6
# File 'lib/hubbado/log/logger.rb', line 4

def log_handlers
  @log_handlers
end

#subjectObject

Returns the value of attribute subject.



5
6
7
# File 'lib/hubbado/log/logger.rb', line 5

def subject
  @subject
end

Instance Method Details

#log(severity, msg, data = nil, tag: nil, tags: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hubbado/log/logger.rb', line 12

def log(severity, msg, data = nil, tag: nil, tags: nil)
  unless SEVERITIES.keys.include? severity.to_sym
    raise ArgumentError, "Unknown serverity #{severity}"
  end

  # Singular and plural are both accepted and both kept, following Eventide's log gem,
  # where real call sites use either and occasionally hand an array to the singular one.
  #
  # Named as symbols, because that is what LOG_TAGS is parsed into: a String here would
  # match nothing and its message would go missing with nothing said about it.
  message_tags = (Array(tags) + Array(tag)).map { |name| name.to_s.to_sym }

  stacktrace = if data.is_a?(Exception)
                 data.full_message
               elsif traced?(severity, message_tags)
                 format_stacktrace Kernel.caller
               end

  log_handlers.each do |handler|
    handler.log(subject, severity, msg, data, stacktrace, message_tags)
  end
end