Class: Hubbado::Log::Logger

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, log_handlers = [], level: nil, tags: nil) ⇒ Logger

Returns a new instance of Logger.



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

def initialize(subject, log_handlers = [], level: nil, tags: nil)
  self.subject = subject
  self.log_handlers = Array(log_handlers)
  @level = level
  @tags = tags
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

#levelObject

A logger built without one follows the configuration, which is every logger the gem builds itself: Log.configure names neither, so a class using the Dependency module takes whatever the process was configured for.



17
# File 'lib/hubbado/log/logger.rb', line 17

def level = @level || Log.config.level

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hubbado/log/logger.rb', line 23

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

  # Read after the severity is checked, never before: the level decides what is printed,
  # not what may be said, so quietening a logger must not turn a typo into silence.
  return if SEVERITIES.fetch(severity.to_sym) < SEVERITIES.fetch(level)

  # 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 }

  # Both filters have to pass. A tag cannot raise a message above the level, and the level
  # cannot rescue one the list leaves out.
  #
  # `self.` because the `tags:` keyword above shadows the reader.
  return unless self.tags.write?(message_tags)

  stacktrace = if data.is_a?(Exception)
                 data.full_message
               elsif STACKTRACE_SEVERITIES.include?(severity)
                 format_stacktrace Kernel.caller
               end

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

#tagsObject

Named per logger as well as per process, as the level is and as Eventide's log gem has it, so one component can be read without turning up everything around it.



21
# File 'lib/hubbado/log/logger.rb', line 21

def tags = @tags.nil? ? Log.config.tags : Tags.parse(@tags)