Module: Legion::Logging::Methods

Includes:
HeaderBuilder
Included in:
Legion::Logging, Logger
Defined in:
lib/legion/logging/methods.rb

Constant Summary collapse

COMPONENT_REGEX =
%r{
  /(runners|actors|actor|helpers|hooks|absorbers|matchers|transport|
  exchanges|queues|messages|data|builders|tools|adapters|engines|
  formatters|parsers|middleware)/
}x

Constants included from HeaderBuilder

HeaderBuilder::EXCEPTION_PRIORITY

Instance Method Summary collapse

Instance Method Details

#debug(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/logging/methods.rb', line 31

def debug(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx)
  return unless log.level < 1

  message = yield if message.nil? && block_given?
  raw = maybe_redact(message)
  formatted = format_message_for_level(:debug, raw)
  with_context_ids(task_id: task_id, conv_id: conv_id, request_id: request_id,
                   exchange_id: exchange_id, chain_id: chain_id) do
    write_async_or_sync(:debug, formatted, raw)
  end
end

#emit_tagged(level, message = nil, segments: nil, method_ctx: nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/legion/logging/methods.rb', line 101

def emit_tagged(level, message = nil, segments: nil, method_ctx: nil,
                task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx)
  level = level.to_sym
  message = yield if message.nil? && block_given?
  return if message.nil?

  raw = maybe_redact(message)
  formatted = format_message_for_level(level, raw)

  with_tagged_context(segments, method_ctx) do
    with_context_ids(task_id: task_id, conv_id: conv_id, request_id: request_id,
                     exchange_id: exchange_id, chain_id: chain_id) do
      ctx = %i[warn error fatal].include?(level) ? build_writer_context(level, raw) : nil
      writer = @async_writer
      caller_trace = capture_runner_trace_for_async
      if writer&.alive?
        writer.push(AsyncWriter::LogEntry.new(
                      level: level, message: formatted, writer_context: ctx,
                      segments: Thread.current[:legion_log_segments],
                      method_ctx: Thread.current[:legion_log_method],
                      caller_trace: caller_trace,
                      conv_id: Thread.current[:legion_log_conv_id],
                      request_id: Thread.current[:legion_log_request_id],
                      exchange_id: Thread.current[:legion_log_exchange_id],
                      chain_id: Thread.current[:legion_log_chain_id]
                    ))
      else
        with_caller_trace(caller_trace) { write_forced(level, formatted) }
        fire_log_writer(level, raw) if ctx
      end
    end
  end
end

#error(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/legion/logging/methods.rb', line 67

def error(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx)
  return unless log.level < 4

  message = yield if message.nil? && block_given?
  raw = maybe_redact(message)
  formatted = format_message_for_level(:error, raw)
  with_context_ids(task_id: task_id, conv_id: conv_id, request_id: request_id,
                   exchange_id: exchange_id, chain_id: chain_id) do
    write_async_or_sync(:error, formatted, raw, writer_context: build_writer_context(:error, raw))
  end
end

#fatal(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/logging/methods.rb', line 79

def fatal(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx)
  return unless log.level < 5

  message = yield if message.nil? && block_given?
  raw = maybe_redact(message)
  formatted = format_message_for_level(:fatal, raw)
  with_context_ids(task_id: task_id, conv_id: conv_id, request_id: request_id,
                   exchange_id: exchange_id, chain_id: chain_id) do
    write_async_or_sync(:fatal, formatted, raw, writer_context: build_writer_context(:fatal, raw))
  end
end

#info(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/logging/methods.rb', line 43

def info(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx)
  return unless log.level < 2

  message = yield if message.nil? && block_given?
  raw = maybe_redact(message)
  formatted = format_message_for_level(:info, raw)
  with_context_ids(task_id: task_id, conv_id: conv_id, request_id: request_id,
                   exchange_id: exchange_id, chain_id: chain_id) do
    write_async_or_sync(:info, formatted, raw)
  end
end

#log_exception(exception, level: :error, lex: nil, component_type: nil, gem_name: nil, lex_version: nil, gem_path: nil, source_code_uri: nil, handled: false, payload_summary: nil, task_id: nil, backtrace_limit: nil, **extra) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/legion/logging/methods.rb', line 140

def log_exception(exception, level: :error, lex: nil, component_type: nil,
                  gem_name: nil, lex_version: nil, gem_path: nil,
                  source_code_uri: nil, handled: false, payload_summary: nil,
                  task_id: nil, backtrace_limit: nil, **extra)
  level = level.to_sym if level.respond_to?(:to_sym)
  # 1. Log human-readable line + backtrace via async writer
  msg = exception.respond_to?(:message) ? exception.message : exception.to_s
  msg = maybe_redact(msg)
  msg = build_exception_log_message(exception, msg, backtrace_limit)
  formatted = format_message_for_level(level, msg)
  write_async_or_sync(level, formatted, msg)

  # 2. Build rich exception event
  event = Legion::Logging::EventBuilder.build_exception(
    exception:       exception,
    level:           level,
    lex:             lex,
    component_type:  component_type,
    gem_name:        gem_name,
    lex_version:     lex_version,
    gem_path:        gem_path,
    source_code_uri: source_code_uri,
    handled:         handled,
    payload_summary: payload_summary,
    task_id:         task_id,
    caller_offset:   3,
    **extra
  )

  # 3. Redact secrets before publishing
  event = Legion::Logging::Redactor.redact(event) if defined?(Legion::Logging::Redactor)

  # 4. Publish rich event via exception_writer
  publish_exception_event(event, level)
rescue StandardError => e
  if respond_to?(:log) && log.respond_to?(:warn)
    log.warn("Failed to publish structured exception event: #{e.class}: #{e.message}")
  else
    warn("Failed to publish structured exception event: #{e.class}: #{e.message}")
  end
end

#runner_exception(exc, **opts) ⇒ Object



135
136
137
138
# File 'lib/legion/logging/methods.rb', line 135

def runner_exception(exc, **opts)
  log_exception(exc, handled: true, **opts)
  { success: false, message: exc.message, backtrace: exc.backtrace }.merge(opts)
end

#thread(kvl: false, **_opts) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/legion/logging/methods.rb', line 182

def thread(kvl: false, **_opts)
  if kvl
    "thread=#{Thread.current.object_id}"
  else
    Thread.current.object_id.to_s
  end
end

#trace(raw_message = nil, size: @trace_size, log_caller: true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/logging/methods.rb', line 17

def trace(raw_message = nil, size: @trace_size, log_caller: true)
  return unless @trace_enabled

  raw_message = yield if raw_message.nil? && block_given?
  message = Rainbow('Tracing: ').cyan
  message.concat Rainbow("#{raw_message} ").cyan
  if log_caller && size.nil?
    message.concat Rainbow(caller_locations).cyan.underline
  elsif log_caller
    message.concat Rainbow(caller_locations[0..size]).cyan.underline
  end
  log.unknown(message)
end

#unknown(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/legion/logging/methods.rb', line 91

def unknown(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx)
  message = yield if message.nil? && block_given?
  raw = maybe_redact(message)
  formatted = format_message_for_level(:unknown, raw)
  with_context_ids(task_id: task_id, conv_id: conv_id, request_id: request_id,
                   exchange_id: exchange_id, chain_id: chain_id) do
    write_async_or_sync(:unknown, formatted, raw)
  end
end

#warn(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/logging/methods.rb', line 55

def warn(message = nil, task_id: nil, conv_id: nil, request_id: nil, exchange_id: nil, chain_id: nil, **_ctx)
  return unless log.level < 3

  message = yield if message.nil? && block_given?
  raw = maybe_redact(message)
  formatted = format_message_for_level(:warn, raw)
  with_context_ids(task_id: task_id, conv_id: conv_id, request_id: request_id,
                   exchange_id: exchange_id, chain_id: chain_id) do
    write_async_or_sync(:warn, formatted, raw, writer_context: build_writer_context(:warn, raw))
  end
end