legion-logging 1.5.1 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/CHANGELOG.md +12 -0
- data/CLAUDE.md +29 -75
- data/README.md +35 -10
- data/lib/legion/logging/helper.rb +11 -6
- data/lib/legion/logging/methods.rb +26 -10
- data/lib/legion/logging/settings.rb +5 -4
- data/lib/legion/logging/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f751e228eb4c00edae10784b3edf4176afb2f84ec7a8df1525a9c8432145c50f
|
|
4
|
+
data.tar.gz: 728db26335f6bffdcef54523760d9b8051615f0bee280ac01a897a3373b3403f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3ba593790cf603ab542162e685c97450f7db2d18da76dd288981317ecf627b708a391e2b0f1eecfef43e3a308f0391a89a6bc636ab3216c6450a973d653b7e3e
|
|
7
|
+
data.tar.gz: 5c6fb87b90d197c3f70e0d69e1d5a3a13c55083aa1f8ebbd79676ff8926b12686e3fa9b2df91286ac2a216ace987863eb719b21df8693e3f0981768a2f70f748
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Legion::Logging Changelog
|
|
2
2
|
|
|
3
|
+
## [1.5.3] - 2026-05-13
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `backtrace_limit:` kwarg on `log_exception` and `handle_exception` (nil=full, 0=suppress, N=cap at N frames)
|
|
7
|
+
- `backtrace_limit` key in `Legion::Logging::Settings.default` (defaults to nil — full backtraces)
|
|
8
|
+
- Settings-driven default reads from `Legion::Settings[:logging][:backtrace_limit]` when no explicit kwarg is passed
|
|
9
|
+
|
|
10
|
+
## [1.5.2] - 2026-04-27
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Exception stdout/file log lines now include the full backtrace instead of truncating after 10 frames with a `... N more` suffix.
|
|
14
|
+
|
|
3
15
|
## [1.5.1] - 2026-04-08
|
|
4
16
|
|
|
5
17
|
### Added
|
data/CLAUDE.md
CHANGED
|
@@ -1,92 +1,46 @@
|
|
|
1
|
-
# legion-logging
|
|
1
|
+
# legion-logging
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
- **Parent**: `/Users/miverso2/rubymine/legion/CLAUDE.md`
|
|
5
|
-
|
|
6
|
-
## Purpose
|
|
7
|
-
|
|
8
|
-
Ruby logging class for the LegionIO framework. Provides colorized console output via Rainbow, structured JSON logging (`format: :json`), and a consistent logging interface across all Legion gems and extensions.
|
|
3
|
+
Structured logging framework for LegionIO. Provides colorized console output (Rainbow), structured JSON logging, and a consistent interface across all Legion gems and extensions.
|
|
9
4
|
|
|
10
5
|
**GitHub**: https://github.com/LegionIO/legion-logging
|
|
11
|
-
**Version**: 1.5.
|
|
12
|
-
**License**: Apache-2.0
|
|
6
|
+
**Version**: 1.5.3
|
|
13
7
|
|
|
14
8
|
## Architecture
|
|
15
9
|
|
|
16
10
|
```
|
|
17
11
|
Legion::Logging (singleton module)
|
|
18
|
-
├── Methods #
|
|
19
|
-
├── Builder # Output destination
|
|
20
|
-
├── AsyncWriter # Non-blocking SizedQueue-backed writer thread
|
|
21
|
-
├── Hooks # Callback registry for fatal/error/warn events
|
|
22
|
-
├── EventBuilder # Structured event payload
|
|
23
|
-
├── Helper # Injectable log mixin for LEX extensions
|
|
24
|
-
├──
|
|
25
|
-
├──
|
|
26
|
-
├──
|
|
27
|
-
├──
|
|
28
|
-
├──
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
# Module-level writers (pluggable lambda slots replacing old Hooks for AMQP forwarding)
|
|
35
|
-
Legion::Logging.log_writer # -> lambda(->(event, routing_key:) {})
|
|
36
|
-
Legion::Logging.exception_writer # -> lambda(->(event, routing_key:, headers:, properties:) {})
|
|
12
|
+
├── Methods # debug, info, warn, error, fatal, unknown; log_exception
|
|
13
|
+
├── Builder # Output destination, log level, formatter, async: keyword
|
|
14
|
+
├── AsyncWriter # Non-blocking SizedQueue-backed writer thread
|
|
15
|
+
├── Hooks # Callback registry for fatal/error/warn events
|
|
16
|
+
├── EventBuilder # Structured event payload + fingerprint for dedup
|
|
17
|
+
├── Helper # Injectable log mixin for LEX extensions
|
|
18
|
+
├── TaggedLogger # Prepends structured tags to each message
|
|
19
|
+
├── CategoryRegistry # Named log categories with expected_fields
|
|
20
|
+
├── SIEMExporter # PHI-redacting SIEM export (Splunk HEC, ELK)
|
|
21
|
+
├── Shipper # Buffered forwarding (FileTransport, HttpTransport)
|
|
22
|
+
├── Redactor # PII/PHI + secret pattern redaction (opt-in)
|
|
23
|
+
└── MultiIO # Write to multiple destinations simultaneously
|
|
24
|
+
|
|
25
|
+
# Module-level writer lambdas (pluggable forwarding slots)
|
|
26
|
+
Legion::Logging.log_writer # ->(event, routing_key:) {}
|
|
27
|
+
Legion::Logging.exception_writer # ->(event, routing_key:, headers:, properties:) {}
|
|
37
28
|
```
|
|
38
29
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
- **Singleton Module**: `Legion::Logging` uses `class << self` — called directly: `Legion::Logging.info("msg")`
|
|
42
|
-
- **Rainbow Colorization**: Console output uses Rainbow gem for colored terminal output. Color auto-disabled in JSON format and when writing to a log file.
|
|
43
|
-
- **Setup Method**: `Legion::Logging.setup(level:, format:, async:, **options)` configures output, level, format, and async mode. Increments `configuration_generation` on each call.
|
|
44
|
-
- **Async by Default**: `setup` enables async logging — calls return immediately. Fatal calls always bypass the queue. `stop_async_writer` flushes and stops on shutdown. Buffer size configurable via `Legion::Settings[:logging][:async][:buffer_size]` (default 10,000). Back-pressure: callers block when buffer is full.
|
|
45
|
-
- **Structured JSON**: `format: :json` in settings outputs machine-parseable JSON log lines (disables color)
|
|
46
|
-
- **Shared Interface**: Same method signature (`info`, `warn`, `error`, etc.) across all Legion components
|
|
47
|
-
- **MultiIO**: Splits writes to stdout and a log file simultaneously (used by Builder when `log_file` is set)
|
|
48
|
-
- **SIEMExporter**: PHI redaction (SSN, phone, MRN, DOB patterns), `export_to_splunk` (HEC), `format_for_elk`
|
|
49
|
-
- **Hook Callbacks**: `on_fatal`, `on_error`, `on_warn` register procs called after each log at those levels. Hooks are gated by `enable_hooks!`/`disable_hooks!`. Hook failures are silently rescued. Hooks fire on the async writer thread; event context captured on caller thread.
|
|
50
|
-
- **EventBuilder**: Builds structured event hashes from log context (caller location, exception info, lex identity, gem metadata). All from in-memory data, zero IO. `fingerprint` produces MD5 for dedup in log aggregation.
|
|
51
|
-
- **Helper mixin**: `Legion::Logging::Helper` is injectable into LEX extensions. Derives logger tags from `segments`, `lex_filename`, or class name. Passes through `settings[:logger]` config when available.
|
|
52
|
-
- **Writer Lambdas**: `log_writer` and `exception_writer` are module-level lambda slots for forwarding to external systems (AMQP, etc.). Default implementations are no-ops. Set via `Legion::Logging.log_writer = lambda`.
|
|
53
|
-
- **CategoryRegistry**: Named log categories with description and expected_fields. Register via `Legion::Logging.register_category`. Used for structured log validation.
|
|
54
|
-
- **Redactor**: Opt-in PII/PHI redaction (`logging.redaction.enabled: true`). Guards against Settings recursive init via `@loader` ivar check. Patterns: SSN, phone, MRN, DOB, Vault tokens, JWTs, bearer tokens, lease IDs.
|
|
30
|
+
## Key Patterns
|
|
55
31
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
| Path | Purpose |
|
|
65
|
-
|------|---------|
|
|
66
|
-
| `lib/legion/logging.rb` | Module entry point |
|
|
67
|
-
| `lib/legion/logging/methods.rb` | Log level methods |
|
|
68
|
-
| `lib/legion/logging/builder.rb` | Output config and formatter (async: keyword) |
|
|
69
|
-
| `lib/legion/logging/async_writer.rb` | Non-blocking SizedQueue-backed writer thread with back-pressure |
|
|
70
|
-
| `lib/legion/logging/helper.rb` | Injectable log mixin for LEX extensions |
|
|
71
|
-
| `lib/legion/logging/logger.rb` | Core logger setup |
|
|
72
|
-
| `lib/legion/logging/multi_io.rb` | Multi-output IO (write to multiple destinations simultaneously) |
|
|
73
|
-
| `lib/legion/logging/siem_exporter.rb` | PHI-redacting SIEM export helpers (Splunk HEC, ELK format) |
|
|
74
|
-
| `lib/legion/logging/hooks.rb` | Callback registry (fatal/error/warn hook arrays, enable/disable/clear) |
|
|
75
|
-
| `lib/legion/logging/event_builder.rb` | Structured event payload builder; `fingerprint` for MD5 dedup |
|
|
76
|
-
| `lib/legion/logging/tagged_logger.rb` | Logger wrapper that prepends structured tags to each message |
|
|
77
|
-
| `lib/legion/logging/category_registry.rb` | Named log category registration and lookup |
|
|
78
|
-
| `lib/legion/logging/method_tracer.rb` | Method call tracing instrumentation (call/return, formatted args) |
|
|
79
|
-
| `lib/legion/logging/shipper.rb` | Buffered log event forwarding to external systems |
|
|
80
|
-
| `lib/legion/logging/shipper/file_transport.rb` | File-based log shipper transport |
|
|
81
|
-
| `lib/legion/logging/shipper/http_transport.rb` | HTTP-based log shipper transport |
|
|
82
|
-
| `lib/legion/logging/siem_exporter.rb` | PHI-redacting SIEM export (Splunk HEC, ELK format) |
|
|
83
|
-
| `lib/legion/logging/redactor.rb` | PII/PHI + secret pattern redaction (opt-in) |
|
|
84
|
-
| `lib/legion/logging/version.rb` | VERSION constant |
|
|
32
|
+
- **Singleton module** — `class << self`; called directly: `Legion::Logging.info("msg")`
|
|
33
|
+
- **Async by default** — `setup` enables async logging; fatal bypasses queue. Buffer size via `Settings[:logging][:async][:buffer_size]` (default 10,000). Back-pressure blocks callers when full.
|
|
34
|
+
- **Structured JSON** — `format: :json` outputs machine-parseable JSON lines (disables color)
|
|
35
|
+
- **Helper mixin** — `Legion::Logging::Helper` injects into LEX extensions; derives tags from `segments`, `lex_filename`, or class name
|
|
36
|
+
- **Writer lambdas** — `log_writer` and `exception_writer` are module-level lambda slots for forwarding to external systems (AMQP, etc.). Default no-ops.
|
|
37
|
+
- **Redactor** — Opt-in (`logging.redaction.enabled: true`). Patterns: SSN, phone, MRN, DOB, Vault tokens, JWTs, bearer tokens, lease IDs. Guards against Settings recursive init.
|
|
38
|
+
- **Hook callbacks** — `on_fatal`, `on_error`, `on_warn` register procs; gated by `enable_hooks!`/`disable_hooks!`; fire on async writer thread
|
|
39
|
+
- **EventBuilder** — Structured event hashes from log context (caller, exception, lex identity). `fingerprint` produces MD5 for dedup.
|
|
85
40
|
|
|
86
41
|
## Role in LegionIO
|
|
87
42
|
|
|
88
|
-
|
|
43
|
+
Foundational gem — dependency of `legion-cache`, `legion-data`, and `LegionIO`. First module initialized during `Legion::Service` startup.
|
|
89
44
|
|
|
90
45
|
---
|
|
91
|
-
|
|
92
46
|
**Maintained By**: Matthew Iverson (@Esity)
|
data/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Logging module for the [LegionIO](https://github.com/LegionIO/LegionIO) framework. Provides colorized console output via Rainbow, structured JSON logging, multi-output IO, and a consistent logging interface across all Legion gems and extensions.
|
|
4
4
|
|
|
5
|
-
**Version**: 1.5.
|
|
5
|
+
**Version**: 1.5.2
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -96,23 +96,46 @@ end
|
|
|
96
96
|
|
|
97
97
|
### Exception Logging
|
|
98
98
|
|
|
99
|
-
`log_exception` provides a single call for complete
|
|
99
|
+
`log_exception` provides a single call for complete exception logging with component context. It writes a human-readable exception line through the configured logger and publishes a structured exception event when an `exception_writer` is configured.
|
|
100
100
|
|
|
101
101
|
```ruby
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
102
|
+
begin
|
|
103
|
+
runner.call
|
|
104
|
+
rescue StandardError => e
|
|
105
|
+
Legion::Logging.log_exception(
|
|
106
|
+
e,
|
|
107
|
+
handled: true,
|
|
108
|
+
component_type: :runner,
|
|
109
|
+
lex: 'my_extension',
|
|
110
|
+
task_id: 'abc-123'
|
|
111
|
+
)
|
|
112
|
+
end
|
|
107
113
|
```
|
|
108
114
|
|
|
115
|
+
The synchronous log line includes the full Ruby backtrace. Legion does not truncate it to a fixed frame count or replace the tail with `... N more`, because the missing frames are often the useful part of production failures.
|
|
116
|
+
|
|
117
|
+
Structured exception events include:
|
|
118
|
+
|
|
119
|
+
- exception class and message
|
|
120
|
+
- full backtrace array
|
|
121
|
+
- caller file, line, and function where available
|
|
122
|
+
- log level and handled/unhandled status
|
|
123
|
+
- component type, lex name, gem name, version, and source path metadata
|
|
124
|
+
- task and thread context
|
|
125
|
+
- stable error fingerprint for deduplication
|
|
126
|
+
|
|
109
127
|
### Writer Lambdas
|
|
110
128
|
|
|
111
129
|
`log_writer` and `exception_writer` are pluggable lambda slots that replace the old Hooks system. Assign them to forward events to external systems:
|
|
112
130
|
|
|
113
131
|
```ruby
|
|
114
|
-
Legion::Logging.exception_writer =
|
|
115
|
-
|
|
132
|
+
Legion::Logging.exception_writer = lambda do |payload, routing_key:, headers:, properties:|
|
|
133
|
+
publish_to_amqp(payload, routing_key:, headers:, properties:)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
Legion::Logging.log_writer = lambda do |context, routing_key:|
|
|
137
|
+
publish_log(context, routing_key:)
|
|
138
|
+
end
|
|
116
139
|
```
|
|
117
140
|
|
|
118
141
|
### EventBuilder
|
|
@@ -121,7 +144,9 @@ Legion::Logging.log_writer = ->(context, routing_key:) { publish_log(context) }
|
|
|
121
144
|
|
|
122
145
|
### Redactor
|
|
123
146
|
|
|
124
|
-
`Legion::Logging::Redactor` redacts PII/PHI patterns (SSN, phone, MRN, DOB) plus Vault tokens, JWTs, bearer tokens, and lease IDs from log messages. Redaction is opt-in: load the module (for example via `require 'legion/logging/redactor'`) and enable it with `logging.redaction.enabled: true`. When loaded and enabled, it is wired into all log methods in the write path.
|
|
147
|
+
`Legion::Logging::Redactor` redacts PII/PHI patterns (SSN, email, phone, MRN, DOB, credit card) plus Vault tokens, JWTs, bearer tokens, `vault://` URIs, `lease://` URIs, and lease IDs from log messages. Redaction is opt-in for text log lines: load the module (for example via `require 'legion/logging/redactor'`) and enable it with `logging.redaction.enabled: true`. When loaded and enabled, it is wired into all log methods in the write path.
|
|
148
|
+
|
|
149
|
+
Structured exception events are redacted before publishing when the redactor is loaded. This includes event identity fields such as `user`, so email-shaped local usernames are not forwarded raw.
|
|
125
150
|
|
|
126
151
|
## Requirements
|
|
127
152
|
|
|
@@ -32,7 +32,6 @@ module Legion
|
|
|
32
32
|
'middleware' => :middleware
|
|
33
33
|
}.freeze
|
|
34
34
|
|
|
35
|
-
EXCEPTION_BACKTRACE_LIMIT = 10
|
|
36
35
|
EXCEPTION_PRIORITY = { warn: 0, error: 5, fatal: 9 }.freeze
|
|
37
36
|
EXCEPTION_COLORS = {
|
|
38
37
|
fatal: :darkred,
|
|
@@ -488,11 +487,17 @@ module Legion
|
|
|
488
487
|
context_line = build_context_line(event)
|
|
489
488
|
lines << " #{context_line}" unless context_line.empty?
|
|
490
489
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
490
|
+
max_frames = if event[:backtrace_limit].nil?
|
|
491
|
+
defined?(Legion::Settings) ? Legion::Settings[:logging][:backtrace_limit] : nil
|
|
492
|
+
else
|
|
493
|
+
event[:backtrace_limit]
|
|
494
|
+
end
|
|
495
|
+
unless max_frames&.zero?
|
|
496
|
+
bt = exception.backtrace
|
|
497
|
+
if bt&.any?
|
|
498
|
+
frames = max_frames ? bt.first(max_frames) : bt
|
|
499
|
+
frames.each { |frame| lines << " #{frame}" }
|
|
500
|
+
end
|
|
496
501
|
end
|
|
497
502
|
|
|
498
503
|
lines.join("\n")
|
|
@@ -102,20 +102,12 @@ module Legion
|
|
|
102
102
|
def log_exception(exception, level: :error, lex: nil, component_type: nil,
|
|
103
103
|
gem_name: nil, lex_version: nil, gem_path: nil,
|
|
104
104
|
source_code_uri: nil, handled: false, payload_summary: nil,
|
|
105
|
-
task_id: nil, **extra)
|
|
105
|
+
task_id: nil, backtrace_limit: nil, **extra)
|
|
106
106
|
level = level.to_sym if level.respond_to?(:to_sym)
|
|
107
107
|
# 1. Log human-readable line + backtrace to stdout/file (bypass writer callbacks)
|
|
108
108
|
msg = exception.respond_to?(:message) ? exception.message : exception.to_s
|
|
109
109
|
msg = maybe_redact(msg)
|
|
110
|
-
|
|
111
|
-
if bt.any?
|
|
112
|
-
limit = defined?(Legion::Logging::Helper::EXCEPTION_BACKTRACE_LIMIT) ? Legion::Logging::Helper::EXCEPTION_BACKTRACE_LIMIT : 10
|
|
113
|
-
lines = ["#{exception.class}: #{msg}"]
|
|
114
|
-
bt.first(limit).each { |frame| lines << " #{frame}" }
|
|
115
|
-
remaining = bt.length - limit
|
|
116
|
-
lines << " ... #{remaining} more" if remaining.positive?
|
|
117
|
-
msg = lines.join("\n")
|
|
118
|
-
end
|
|
110
|
+
msg = build_exception_log_message(exception, msg, backtrace_limit)
|
|
119
111
|
log.public_send(level, msg) if respond_to?(:log) && log.respond_to?(level)
|
|
120
112
|
|
|
121
113
|
# 2. Build rich exception event
|
|
@@ -158,6 +150,30 @@ module Legion
|
|
|
158
150
|
|
|
159
151
|
private
|
|
160
152
|
|
|
153
|
+
def resolve_backtrace_limit(explicit_limit)
|
|
154
|
+
return explicit_limit unless explicit_limit.nil?
|
|
155
|
+
return nil unless defined?(Legion::Settings)
|
|
156
|
+
|
|
157
|
+
Legion::Settings[:logging][:backtrace_limit]
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def build_exception_log_message(exception, msg, backtrace_limit)
|
|
161
|
+
max_frames = resolve_backtrace_limit(backtrace_limit)
|
|
162
|
+
bt = collect_backtrace_frames(exception, max_frames)
|
|
163
|
+
return msg unless bt.any?
|
|
164
|
+
|
|
165
|
+
lines = ["#{exception.class}: #{msg}"]
|
|
166
|
+
bt.each { |frame| lines << " #{frame}" }
|
|
167
|
+
lines.join("\n")
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def collect_backtrace_frames(exception, max_frames)
|
|
171
|
+
return [] if max_frames&.zero?
|
|
172
|
+
|
|
173
|
+
frames = Array(exception.backtrace)
|
|
174
|
+
max_frames ? frames.first(max_frames) : frames
|
|
175
|
+
end
|
|
176
|
+
|
|
161
177
|
def maybe_redact(message)
|
|
162
178
|
return message unless message.is_a?(String)
|
|
163
179
|
return message unless redaction_enabled?
|