Module: Legion::Logging::Helper

Defined in:
lib/legion/logging/helper.rb

Constant Summary collapse

SEGMENT_CACHE =

rubocop:disable Style/MutableConstant

{}
COMPONENT_MAP =
{
  'runners'    => :runner,
  'actors'     => :actor,
  'actor'      => :actor,
  'helpers'    => :helper,
  'hooks'      => :hook,
  'absorbers'  => :absorber,
  'matchers'   => :matcher,
  'transport'  => :transport,
  'exchanges'  => :exchange,
  'queues'     => :queue,
  'messages'   => :message,
  'data'       => :data,
  'builders'   => :builder,
  'tools'      => :tool,
  'adapters'   => :adapter,
  'engines'    => :engine,
  'formatters' => :formatter,
  'parsers'    => :parser,
  'middleware' => :middleware
}.freeze
EXCEPTION_PRIORITY =
{ warn: 0, error: 5, fatal: 9 }.freeze
EXCEPTION_COLORS =
{
  fatal:   :darkred,
  error:   :red,
  warn:    :yellow,
  debug:   :aqua,
  unknown: :magenta
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_contextObject



52
53
54
# File 'lib/legion/logging/helper.rb', line 52

def self.current_context
  Thread.current[:legion_context]
end

.current_log_methodObject



44
45
46
# File 'lib/legion/logging/helper.rb', line 44

def self.current_log_method
  Thread.current[:legion_log_method]
end

.current_log_segmentsObject



48
49
50
# File 'lib/legion/logging/helper.rb', line 48

def self.current_log_segments
  Thread.current[:legion_log_segments]
end

.extended(base) ⇒ Object



109
110
111
# File 'lib/legion/logging/helper.rb', line 109

def self.extended(base)
  MethodTracer.attach(base, match_singleton: true) if defined?(MethodTracer) && MethodTracer::ENABLED
end

.included(base) ⇒ Object



105
106
107
# File 'lib/legion/logging/helper.rb', line 105

def self.included(base)
  MethodTracer.attach(base) if defined?(MethodTracer) && MethodTracer::ENABLED
end

Instance Method Details

#handle_exception(exception, task_id: nil, level: :error, handled: true, **opts) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/legion/logging/helper.rb', line 80

def handle_exception(exception, task_id: nil, level: :error, handled: true, **opts)
  segments = derive_log_segments
  spec = gem_spec
  ctx = Thread.current[:legion_context] || {}

  event = build_exception_event(
    exception:       exception,
    level:           level,
    spec:            spec,
    handled:         handled,
    task_id:         task_id || ctx[:task_id],
    payload_summary: opts.empty? ? nil : opts
  )

  event[:conversation_id] ||= ctx[:conversation_id]
  event[:chain_id] ||= ctx[:chain_id]
  event[:log_segments] = segments
  event[:method] = Thread.current[:legion_log_method]

  event = Legion::Logging::Redactor.redact(event) if defined?(Legion::Logging::Redactor)

  write_exception_to_log(exception, event, level, segments)
  publish_exception(event, level) if structured_exception_support?
end

#logObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/legion/logging/helper.rb', line 56

def log
  current_generation =
    if defined?(Legion::Logging) && Legion::Logging.respond_to?(:configuration_generation)
      Legion::Logging.configuration_generation
    else
      0
    end

  if !defined?(@log) || @log.nil? || @log_generation != current_generation
    @log = Legion::Logging::TaggedLogger.new(segments: derive_log_segments, **tagged_logger_settings)
    @log_generation = current_generation
  end

  @log
end

#with_log_context(method_name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/legion/logging/helper.rb', line 72

def with_log_context(method_name)
  prev = Thread.current[:legion_log_method]
  Thread.current[:legion_log_method] = method_name.to_s
  yield
ensure
  Thread.current[:legion_log_method] = prev
end