philiprehberger-structured_logger 0.3.4 → 0.4.0
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/CHANGELOG.md +10 -0
- data/README.md +18 -0
- data/lib/philiprehberger/structured_logger/logger.rb +35 -0
- data/lib/philiprehberger/structured_logger/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dc7adb741998f54fcb27d66e78322d0abcb77c6537298e8b8d9be58b999bf571
|
|
4
|
+
data.tar.gz: bbeea6cf1479ffa7548be5ab32cdaca87dc42c8d5657a778e1e71574056ac4f1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 67ac2a22e4a7b1b3397256fc39e9e04d11fe219b21096fd1b3c8e9996bb98f8120859baa378448d33cfe55425759c406aac72f17520582fcda4ad23e4171c4a2
|
|
7
|
+
data.tar.gz: db854b806432b15b8b891592011f02ca39741975657ef1948d6fcfad6f5408e69101b7100b46007b03588216fd85cb997c7d91fbfa299d99dcff82c88fdb5ace
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.4.0] - 2026-04-17
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `Logger#measure(event, **context) { block }` yields to the block, captures elapsed time, and emits a single info-level structured event with `duration_ms` (plus `error`/`error_class` on failure; original exception re-raised)
|
|
14
|
+
|
|
15
|
+
## [0.3.5] - 2026-03-31
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
- Add GitHub issue templates, dependabot config, and PR template
|
|
19
|
+
|
|
10
20
|
## [0.3.4] - 2026-03-31
|
|
11
21
|
|
|
12
22
|
### Changed
|
data/README.md
CHANGED
|
@@ -121,6 +121,23 @@ end
|
|
|
121
121
|
logger.log_exception(e, level: :fatal, user_id: 42)
|
|
122
122
|
```
|
|
123
123
|
|
|
124
|
+
### Timing a block
|
|
125
|
+
|
|
126
|
+
Use `measure` to time a block and emit a single structured event with `duration_ms`:
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
logger.measure("db.query", table: "users") { User.find(1) }
|
|
130
|
+
# => {"timestamp":"...","level":"info","message":"db.query","event":"db.query","table":"users","duration_ms":12.345}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
On failure, the original exception is re-raised and the log entry also includes `error` and `error_class`:
|
|
134
|
+
|
|
135
|
+
```ruby
|
|
136
|
+
logger.measure("db.query") { raise "boom" }
|
|
137
|
+
# => {"timestamp":"...","level":"info","message":"db.query","event":"db.query","duration_ms":0.123,"error":"boom","error_class":"RuntimeError"}
|
|
138
|
+
# RuntimeError: boom
|
|
139
|
+
```
|
|
140
|
+
|
|
124
141
|
### Multiple Outputs
|
|
125
142
|
|
|
126
143
|
Log to multiple destinations simultaneously. Each output can have its own level filter and formatter:
|
|
@@ -257,6 +274,7 @@ When the buffer is full, writes fall back to synchronous mode (backpressure) to
|
|
|
257
274
|
| `with_context(**extra, &block)` | Temporarily merge context for a block |
|
|
258
275
|
| `silence(level = :fatal, &block)` | Temporarily raise log level for a block |
|
|
259
276
|
| `log_exception(exception, level: :error, **extra)` | Log exception details |
|
|
277
|
+
| `measure(event_name, **context) { block }` | Time a block, emit an info event with `duration_ms`, and re-raise on failure |
|
|
260
278
|
| `add_output(io, level: nil, formatter: nil)` | Add an output destination at runtime |
|
|
261
279
|
| `with_correlation_id(id = nil, &block)` | Set a correlation ID for the block |
|
|
262
280
|
| `flush` | Force write of all buffered log entries |
|
|
@@ -76,6 +76,41 @@ module Philiprehberger
|
|
|
76
76
|
**extra)
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
+
# Yields to the given block, measures its monotonic wall-clock
|
|
80
|
+
# duration, and emits a single info-level log entry describing the
|
|
81
|
+
# outcome. On success, the block's return value is returned. On
|
|
82
|
+
# exception, the failure is logged and the original exception is
|
|
83
|
+
# re-raised.
|
|
84
|
+
#
|
|
85
|
+
# @param event_name [String, Symbol] the event name to record as
|
|
86
|
+
# the `event` field in the log entry.
|
|
87
|
+
# @param context [Hash] extra context merged into the log entry.
|
|
88
|
+
# @yield executes the measured block.
|
|
89
|
+
# @return [Object] the block's return value on success.
|
|
90
|
+
# @raise re-raises any exception raised by the block.
|
|
91
|
+
#
|
|
92
|
+
# @example Measuring a database query
|
|
93
|
+
# logger.measure('db.query', table: 'users') { User.find(1) }
|
|
94
|
+
# # logs event: 'db.query', table: 'users', duration_ms: 12.345
|
|
95
|
+
def measure(event_name, **context)
|
|
96
|
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
97
|
+
begin
|
|
98
|
+
result = yield
|
|
99
|
+
duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000.0).round(3)
|
|
100
|
+
log(:info, event_name.to_s, event: event_name, duration_ms: duration_ms, **context)
|
|
101
|
+
result
|
|
102
|
+
rescue StandardError => e
|
|
103
|
+
duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000.0).round(3)
|
|
104
|
+
log(:info, event_name.to_s,
|
|
105
|
+
event: event_name,
|
|
106
|
+
duration_ms: duration_ms,
|
|
107
|
+
error: e.message,
|
|
108
|
+
error_class: e.class.name,
|
|
109
|
+
**context)
|
|
110
|
+
raise
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
79
114
|
def flush
|
|
80
115
|
@monitor.synchronize { @outputs.each { |out| out[:io].flush if out[:io].respond_to?(:flush) } }
|
|
81
116
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: philiprehberger-structured_logger
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Philip Rehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-04-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A zero-dependency Ruby gem for structured JSON logging with context merging,
|
|
14
14
|
child loggers, level filtering, and pluggable outputs.
|
|
@@ -26,11 +26,11 @@ files:
|
|
|
26
26
|
- lib/philiprehberger/structured_logger/formatter.rb
|
|
27
27
|
- lib/philiprehberger/structured_logger/logger.rb
|
|
28
28
|
- lib/philiprehberger/structured_logger/version.rb
|
|
29
|
-
homepage: https://
|
|
29
|
+
homepage: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-structured_logger
|
|
30
30
|
licenses:
|
|
31
31
|
- MIT
|
|
32
32
|
metadata:
|
|
33
|
-
homepage_uri: https://
|
|
33
|
+
homepage_uri: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-structured_logger
|
|
34
34
|
source_code_uri: https://github.com/philiprehberger/rb-structured-logger
|
|
35
35
|
changelog_uri: https://github.com/philiprehberger/rb-structured-logger/blob/main/CHANGELOG.md
|
|
36
36
|
rubygems_mfa_required: 'true'
|