Class: Errsight::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/errsight/logger.rb

Overview

A Logger-compatible class that forwards log entries to Errsight and optionally delegates to a backing logger.

Constant Summary collapse

LEVEL_MAP =
{
  DEBUG   => :debug,
  INFO    => :info,
  WARN    => :warning,
  ERROR   => :error,
  FATAL   => :fatal,
  UNKNOWN => :info
}.freeze
LEVEL_ORDER =
%i[debug info warning error fatal].freeze

Instance Method Summary collapse

Constructor Details

#initialize(backing_logger = nil) ⇒ Logger

Returns a new instance of Logger.



18
19
20
21
# File 'lib/errsight/logger.rb', line 18

def initialize(backing_logger = nil)
  super(IO::NULL)
  @backing_logger = backing_logger
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log



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
# File 'lib/errsight/logger.rb', line 23

def add(severity, message = nil, progname = nil, &block)
  # Forward to the backing logger if present
  @backing_logger&.add(severity, message, progname, &block)

  # Cheap early-return: skip all allocations when Errsight is disabled
  # or this severity is below the configured threshold. Rails.logger can
  # fire thousands of times per request at :debug; this path matters.
  config = Errsight.configuration
  return true unless config.enabled?

  level = LEVEL_MAP[severity] || :info
  return true if LEVEL_ORDER.index(level).to_i < LEVEL_ORDER.index(config.min_level).to_i

  message = block_given? ? yield : message
  message ||= progname
  return true if message.nil?

   = {}
  if (request_id = Thread.current[:errsight_request_id])
    [:request_id] = request_id
  end

  Errsight.log(level: level, message: message.to_s, metadata: )
  true
end