Module: Alplus::LoggerBreadcrumbs

Defined in:
lib/alplus/logger_breadcrumbs.rb

Overview

Records application log lines as breadcrumbs (issue #47): every Logger#add on an attached logger becomes a log-category breadcrumb on the current thread's Scope ring (before-error context) and is offered to any pending exception item inside its post-error log window (after-error context). Attached to Rails.logger by the Railtie; usable on any stdlib-compatible logger via attach.

Excluded on purpose: the SDK's own [alplus] diagnostics (the timeline must hold the application's output, not the SDK talking to itself), and non-String messages (the block form is not evaluated here — evaluating it early would change the host's lazy-logging semantics).

Defined Under Namespace

Modules: Recorder

Constant Summary collapse

SEVERITY_LEVEL =
{
  0 => "debug", 1 => "info", 2 => "warning", 3 => "error", 4 => "fatal"
}.freeze
SDK_INTERNAL_PREFIX =
"[alplus]"

Class Method Summary collapse

Class Method Details

.attach(logger) ⇒ Object



33
34
35
36
37
# File 'lib/alplus/logger_breadcrumbs.rb', line 33

def attach(logger)
  return if logger.nil? || logger.singleton_class.ancestors.include?(Recorder)

  logger.singleton_class.prepend(Recorder)
end

.record(severity, message, progname) ⇒ Object

Never raises, and never re-enters (a breadcrumb path that itself logs would otherwise recurse through the patched add).



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alplus/logger_breadcrumbs.rb', line 41

def record(severity, message, progname)
  config = Alplus.configuration
  return unless config&.logger_breadcrumbs_enabled
  return if Thread.current[:alplus_recording_log_breadcrumb]

  text = message.is_a?(String) ? message : (progname if progname.is_a?(String) && message.nil?)
  return if text.nil? || text.empty? || text.start_with?(SDK_INTERNAL_PREFIX)

  Thread.current[:alplus_recording_log_breadcrumb] = true
  level = SEVERITY_LEVEL.fetch(severity, "info")
  Scope.current.add_breadcrumb(category: "log", message: text, level: level)
  Alplus.initialized_client&.notify_log_breadcrumb(
    category: "log",
    message: Envelope.cap_text(text, Envelope::MAX_BREADCRUMB_MESSAGE_CHARS),
    level: level,
    ts: Time.now.utc.iso8601(3)
  )
rescue StandardError
  nil
ensure
  Thread.current[:alplus_recording_log_breadcrumb] = nil
end