semantic_logger 5.1.0 → 5.3.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/README.md +13 -12
- data/Rakefile +15 -4
- data/docs/api.md +196 -19
- data/docs/appenders.md +579 -68
- data/docs/config.md +647 -38
- data/docs/index.md +31 -5
- data/docs/log.md +56 -17
- data/docs/metrics.md +144 -30
- data/docs/operations.md +339 -24
- data/docs/rails.md +396 -8
- data/docs/security.md +222 -27
- data/docs/testing.md +28 -3
- data/docs/upgrading.md +938 -7
- data/lib/semantic_logger/appender/async.rb +42 -11
- data/lib/semantic_logger/appender/bugsnag.rb +7 -2
- data/lib/semantic_logger/appender/cloudwatch_logs.rb +168 -33
- data/lib/semantic_logger/appender/elasticsearch_base.rb +221 -28
- data/lib/semantic_logger/appender/elasticsearch_http.rb +23 -6
- data/lib/semantic_logger/appender/file.rb +160 -37
- data/lib/semantic_logger/appender/graylog.rb +101 -11
- data/lib/semantic_logger/appender/honeybadger.rb +7 -1
- data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
- data/lib/semantic_logger/appender/http.rb +308 -46
- data/lib/semantic_logger/appender/io.rb +11 -6
- data/lib/semantic_logger/appender/kafka.rb +204 -44
- data/lib/semantic_logger/appender/loki.rb +55 -9
- data/lib/semantic_logger/appender/mongodb.rb +109 -5
- data/lib/semantic_logger/appender/new_relic.rb +3 -4
- data/lib/semantic_logger/appender/new_relic_logs.rb +35 -15
- data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
- data/lib/semantic_logger/appender/rabbitmq.rb +246 -17
- data/lib/semantic_logger/appender/sentry.rb +3 -4
- data/lib/semantic_logger/appender/sentry_ruby.rb +159 -18
- data/lib/semantic_logger/appender/splunk.rb +11 -2
- data/lib/semantic_logger/appender/splunk_http.rb +92 -1
- data/lib/semantic_logger/appender/syslog.rb +149 -32
- data/lib/semantic_logger/appender/tcp.rb +73 -98
- data/lib/semantic_logger/appender/text_output.rb +65 -0
- data/lib/semantic_logger/appender/udp.rb +140 -18
- data/lib/semantic_logger/appender/wrapper.rb +10 -7
- data/lib/semantic_logger/appender.rb +115 -10
- data/lib/semantic_logger/appenders.rb +145 -5
- data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
- data/lib/semantic_logger/base.rb +115 -81
- data/lib/semantic_logger/call_site.rb +235 -0
- data/lib/semantic_logger/context.rb +222 -0
- data/lib/semantic_logger/core_ext/process.rb +20 -5
- data/lib/semantic_logger/core_ext/warning.rb +70 -0
- data/lib/semantic_logger/deprecation.rb +139 -0
- data/lib/semantic_logger/exception_details.rb +51 -0
- data/lib/semantic_logger/formatters/base.rb +186 -20
- data/lib/semantic_logger/formatters/color.rb +18 -3
- data/lib/semantic_logger/formatters/default.rb +53 -8
- data/lib/semantic_logger/formatters/ecs.rb +9 -4
- data/lib/semantic_logger/formatters/json.rb +9 -0
- data/lib/semantic_logger/formatters/logfmt.rb +155 -21
- data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
- data/lib/semantic_logger/formatters/loki.rb +430 -38
- data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
- data/lib/semantic_logger/formatters/one_line.rb +101 -3
- data/lib/semantic_logger/formatters/open_telemetry.rb +191 -35
- data/lib/semantic_logger/formatters/pattern.rb +68 -12
- data/lib/semantic_logger/formatters/raw.rb +40 -11
- data/lib/semantic_logger/formatters/signalfx.rb +55 -15
- data/lib/semantic_logger/formatters/syslog.rb +114 -20
- data/lib/semantic_logger/formatters/syslog_cee.rb +38 -19
- data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
- data/lib/semantic_logger/formatters.rb +1 -0
- data/lib/semantic_logger/levels.rb +10 -1
- data/lib/semantic_logger/log.rb +384 -82
- data/lib/semantic_logger/loggable.rb +56 -73
- data/lib/semantic_logger/logger.rb +31 -5
- data/lib/semantic_logger/metric/new_relic.rb +33 -18
- data/lib/semantic_logger/metric/signalfx.rb +29 -18
- data/lib/semantic_logger/metric/statsd.rb +41 -15
- data/lib/semantic_logger/processor.rb +194 -5
- data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
- data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
- data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
- data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
- data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
- data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
- data/lib/semantic_logger/queue_processor/request.rb +93 -0
- data/lib/semantic_logger/queue_processor/throttled_counter.rb +89 -0
- data/lib/semantic_logger/queue_processor.rb +519 -170
- data/lib/semantic_logger/ractor_copy.rb +120 -0
- data/lib/semantic_logger/ractor_processor.rb +82 -0
- data/lib/semantic_logger/ractor_receiver.rb +282 -0
- data/lib/semantic_logger/ractor_support.rb +28 -0
- data/lib/semantic_logger/redactor.rb +310 -0
- data/lib/semantic_logger/semantic_logger.rb +328 -81
- data/lib/semantic_logger/subscriber.rb +250 -10
- data/lib/semantic_logger/sync_processor.rb +237 -26
- data/lib/semantic_logger/test/capture_log_events.rb +26 -5
- data/lib/semantic_logger/test/minitest.rb +2 -1
- data/lib/semantic_logger/test/rspec.rb +1 -1
- data/lib/semantic_logger/trap_safe.rb +113 -0
- data/lib/semantic_logger/utils.rb +482 -67
- data/lib/semantic_logger/version.rb +1 -1
- data/lib/semantic_logger.rb +36 -3
- metadata +41 -5
- data/lib/semantic_logger/core_ext/thread.rb +0 -30
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bfb8a2265081eac50cc50aa7044a5d51b277fba016cb11eb76166d0c9216fe2b
|
|
4
|
+
data.tar.gz: 678e62939127794a93e02280e9e0ed898af39a6015118bdba1f652a65abc4a2f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a32ccdf8c3801b3e5166c6d3be83d9c8423defbd2b65e50dc11c262f7cab8c1b0b864a19e90ba9415f2e66d0548a45c0979cb42a03e93252f1381c87634289bc
|
|
7
|
+
data.tar.gz: adab42f9b9e25495ea3a9b99c3322a30a95e4e7eb767f85d27a147831f30861053f08fe22d33318b77c805bf70d301e6c2adcff869cf4ea002607140b68c5ea8
|
data/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# Semantic Logger
|
|
2
|
-
[](https://rubygems.org/gems/semantic_logger) [](https://github.com/reidmorrison/semantic_logger/actions?query=workflow%3Abuild) [](https://rubygems.org/gems/semantic_logger) [](http://opensource.org/licenses/Apache-2.0) 
|
|
2
|
+
[](https://rubygems.org/gems/semantic_logger) [](https://github.com/reidmorrison/semantic_logger/actions?query=workflow%3Abuild) [](https://rubygems.org/gems/semantic_logger) [](https://github.com/reidmorrison/semantic_logger/actions/workflows/ci.yml) [](http://opensource.org/licenses/Apache-2.0) 
|
|
3
3
|
|
|
4
4
|
Semantic Logger is a high-performance, asynchronous structured logging framework for Ruby
|
|
5
5
|
and Rails.
|
|
@@ -38,12 +38,12 @@ replaces the Rails default logger with Semantic Logger automatically.
|
|
|
38
38
|
|
|
39
39
|
## Documentation
|
|
40
40
|
|
|
41
|
-
Start with the [Introduction](https://logger.
|
|
42
|
-
[Programmer's Guide](https://logger.
|
|
41
|
+
Start with the [Introduction](https://logger.reidmorrison.com/), then the
|
|
42
|
+
[Programmer's Guide](https://logger.reidmorrison.com/api.html).
|
|
43
43
|
|
|
44
|
-
* Full guide: [https://logger.
|
|
45
|
-
* For AI assistants: [llms.txt](https://logger.
|
|
46
|
-
[llms-full.txt](https://logger.
|
|
44
|
+
* Full guide: [https://logger.reidmorrison.com/](https://logger.reidmorrison.com/)
|
|
45
|
+
* For AI assistants: [llms.txt](https://logger.reidmorrison.com/llms.txt) (documentation index) and
|
|
46
|
+
[llms-full.txt](https://logger.reidmorrison.com/llms-full.txt) (complete documentation in one file).
|
|
47
47
|
The same pages are also included as markdown in the installed gem, under `docs/`.
|
|
48
48
|
|
|
49
49
|
## Logging Destinations
|
|
@@ -82,7 +82,7 @@ instead of Semantic Logger directly since it will automatically replace the Rail
|
|
|
82
82
|
|
|
83
83
|
## Rocket Job
|
|
84
84
|
|
|
85
|
-
Checkout the sister project [Rocket Job](
|
|
85
|
+
Checkout the sister project [Rocket Job](https://rocketjob.reidmorrison.com): Ruby's missing batch system.
|
|
86
86
|
|
|
87
87
|
Fully supports Semantic Logger when running jobs in the background. Complete support for job metrics
|
|
88
88
|
sent via Semantic Logger to your favorite dashboards.
|
|
@@ -97,9 +97,10 @@ and are therefore not automatically included by this gem:
|
|
|
97
97
|
- HoneybadgerInsights Appender: gem 'honeybadger'
|
|
98
98
|
- NewRelic Appender: gem 'newrelic_rpm'
|
|
99
99
|
- NewRelicLogs Appender: gem 'newrelic_rpm'
|
|
100
|
-
- Syslog Appender: gem '
|
|
101
|
-
- Syslog Appender to a remote
|
|
102
|
-
-
|
|
100
|
+
- Syslog Appender to a local syslog daemon: gem 'syslog' (a bundled gem since Ruby 3.4)
|
|
101
|
+
- Syslog Appender to a remote syslog server over TCP or UDP: gem 'syslog_protocol' 0.9.2 or above
|
|
102
|
+
- Syslog Appender to a remote syslog server over TCP: gem 'net_tcp_client'
|
|
103
|
+
- Splunk Appender (deprecated, use the Splunk HTTP appender): gem 'splunk-sdk-ruby'
|
|
103
104
|
- Elasticsearch Appender: gem 'elasticsearch'
|
|
104
105
|
- OpenSearch Appender: gem 'opensearch-ruby'
|
|
105
106
|
- Kafka Appender: gem 'ruby-kafka'
|
|
@@ -109,7 +110,7 @@ and are therefore not automatically included by this gem:
|
|
|
109
110
|
|
|
110
111
|
## Upgrading
|
|
111
112
|
|
|
112
|
-
See the [Upgrading Guide](https://logger.
|
|
113
|
+
See the [Upgrading Guide](https://logger.reidmorrison.com/upgrading.html) for instructions on
|
|
113
114
|
upgrading between major versions.
|
|
114
115
|
|
|
115
116
|
## Install
|
|
@@ -128,7 +129,7 @@ SemanticLogger.default_level = :trace
|
|
|
128
129
|
SemanticLogger.add_appender(file_name: 'development.log', formatter: :color)
|
|
129
130
|
~~~
|
|
130
131
|
|
|
131
|
-
If running rails, see: [Semantic Logger Rails](https://logger.
|
|
132
|
+
If running rails, see: [Semantic Logger Rails](https://logger.reidmorrison.com/rails.html)
|
|
132
133
|
|
|
133
134
|
## Contributing
|
|
134
135
|
|
data/Rakefile
CHANGED
|
@@ -25,19 +25,30 @@ task :llms_full do
|
|
|
25
25
|
|
|
26
26
|
> Semantic Logger is a high-performance, asynchronous structured logging framework for Ruby and Rails.
|
|
27
27
|
|
|
28
|
-
This file concatenates every page of https://logger.
|
|
28
|
+
This file concatenates every page of https://logger.reidmorrison.com for consumption by AI assistants.
|
|
29
29
|
It is generated from the markdown sources in docs/ by `bundle exec rake llms_full`; do not edit it directly.
|
|
30
|
-
A per-page index is available at https://logger.
|
|
30
|
+
A per-page index is available at https://logger.reidmorrison.com/llms.txt
|
|
31
31
|
HEADER
|
|
32
32
|
|
|
33
33
|
sections = pages.map do |page|
|
|
34
|
-
|
|
34
|
+
raw = File.read("docs/#{page}.md")
|
|
35
|
+
|
|
36
|
+
# The page title lives in front matter, which the shared docs theme renders
|
|
37
|
+
# as the h1. This file strips front matter, so lift the title back out and
|
|
38
|
+
# re-emit it as a heading; without this every section here would open with
|
|
39
|
+
# no indication of which page it is.
|
|
40
|
+
front_matter = raw[/\A---\n(.*?)\n---\n/m, 1].to_s
|
|
41
|
+
title = front_matter[/^(?:title|heading):[ \t]*(.+)$/, 1].to_s.strip.delete_prefix('"').delete_suffix('"')
|
|
42
|
+
|
|
43
|
+
text = raw.
|
|
35
44
|
sub(/\A---\n.*?\n---\n/m, ""). # Jekyll front matter
|
|
36
45
|
gsub(/^\{:.*\}\n/, ""). # kramdown attribute lines ({:toc}, {:.no_toc}, ...)
|
|
37
46
|
gsub(/^\* TOC\n/, "").
|
|
38
47
|
gsub(/^\*\*Contents\*\*\n/, "").
|
|
39
48
|
gsub(/^!\[.*\n/, "") # images (relative paths, useless in plain text)
|
|
40
|
-
|
|
49
|
+
|
|
50
|
+
body = title.empty? ? text.strip : "## #{title}\n\n#{text.strip}"
|
|
51
|
+
"<!-- source: docs/#{page}.md -->\n\n#{body}\n"
|
|
41
52
|
end
|
|
42
53
|
|
|
43
54
|
File.write("docs/llms-full.txt", ([header] + sections).join("\n\n---\n\n"))
|
data/docs/api.md
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
---
|
|
2
2
|
layout: default
|
|
3
|
+
title: Programming Guide
|
|
4
|
+
description: >-
|
|
5
|
+
How to log with Semantic Logger: getting a logger, log levels, structured
|
|
6
|
+
payloads, exceptions, the block form, measuring durations, and tagging
|
|
7
|
+
related entries.
|
|
3
8
|
---
|
|
4
9
|
|
|
5
|
-
## Programming Guide
|
|
6
|
-
{:.no_toc}
|
|
7
10
|
|
|
8
11
|
**Contents**
|
|
9
12
|
|
|
@@ -64,6 +67,30 @@ Every entry is identified as coming from `Supplier`:
|
|
|
64
67
|
2012-08-30 15:37:29.474 I [48308:main] Supplier -- Accessible from instance methods
|
|
65
68
|
~~~
|
|
66
69
|
|
|
70
|
+
A module can include `SemanticLogger::Loggable` too. The module gets a `logger` of its own, and its
|
|
71
|
+
instance methods get a `logger` in any class that includes the module, whether or not that class
|
|
72
|
+
includes `SemanticLogger::Loggable`:
|
|
73
|
+
|
|
74
|
+
~~~ruby
|
|
75
|
+
module Pricing
|
|
76
|
+
include SemanticLogger::Loggable
|
|
77
|
+
|
|
78
|
+
def price(amount)
|
|
79
|
+
logger.info("Pricing", amount: amount)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
class Order
|
|
84
|
+
include Pricing
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
Order.new.price(10)
|
|
88
|
+
~~~
|
|
89
|
+
|
|
90
|
+
The entry is identified as coming from `Order`, the class that called `price`. When that class has a
|
|
91
|
+
class level `logger`, such as one from `SemanticLogger::Loggable`, the module's methods use it instead.
|
|
92
|
+
When the class itself defines a `logger` instance method, they use that.
|
|
93
|
+
|
|
67
94
|
By default `SemanticLogger[...]` returns a brand new logger on each call. To share a single logger
|
|
68
95
|
per class instead, enable [logger caching](config.html#caching-loggers).
|
|
69
96
|
|
|
@@ -173,8 +200,25 @@ logger.error(message: "Calling Supplier", duration: 100)
|
|
|
173
200
|
|
|
174
201
|
# Attach a metric (see Step 6)
|
|
175
202
|
logger.error(message: "Calling Supplier", metric: "Supplier/inquiry", metric_amount: 21)
|
|
203
|
+
|
|
204
|
+
# Attach a backtrace of your own, an Array of Strings, instead of the logger's call stack
|
|
205
|
+
logger.warn(message: "Open redirect detected", backtrace: redirect_stack)
|
|
206
|
+
|
|
207
|
+
# Say where the entry was logged, or that it has no such place (see call_site_level)
|
|
208
|
+
logger.debug(message: "Order Load", duration: 0.4, call_site: query_location)
|
|
209
|
+
logger.info(message: "Started GET /orders", call_site: false)
|
|
176
210
|
~~~
|
|
177
211
|
|
|
212
|
+
In both forms, a key such as `duration` or `exception` is only used as an argument when its value is
|
|
213
|
+
valid for it; otherwise it is logged as part of the payload. Any other keys supplied alongside
|
|
214
|
+
`payload:` are merged into it, without overwriting its own values. When the Hash holds untrusted
|
|
215
|
+
data, see [Logging untrusted hashes](security.html#logging-untrusted-hashes).
|
|
216
|
+
|
|
217
|
+
A block can return the same Hash, but it never replaces an argument the entry already has. That
|
|
218
|
+
includes the backtrace that an entry captures when its level is at or above
|
|
219
|
+
`SemanticLogger.backtrace_level`, so to attach a backtrace of your own, pass `backtrace:` with the
|
|
220
|
+
call rather than returning it from the block.
|
|
221
|
+
|
|
178
222
|
## Step 6: Measure how long something takes
|
|
179
223
|
|
|
180
224
|
It is good practice to "measure everything" in production, so that when things slow down it is
|
|
@@ -228,6 +272,17 @@ logger.measure_info("Called external interface", metric: "Supplier/inquiry") do
|
|
|
228
272
|
end
|
|
229
273
|
~~~
|
|
230
274
|
|
|
275
|
+
Add `dimensions` to break the metric down, such as by supplier. See
|
|
276
|
+
[Break a metric down with dimensions](metrics.html#step-3-break-a-metric-down-with-dimensions):
|
|
277
|
+
|
|
278
|
+
~~~ruby
|
|
279
|
+
logger.measure_info("Called external interface",
|
|
280
|
+
metric: "Supplier/inquiry",
|
|
281
|
+
dimensions: {supplier: "acme"}) do
|
|
282
|
+
# ...
|
|
283
|
+
end
|
|
284
|
+
~~~
|
|
285
|
+
|
|
231
286
|
### Supply the duration yourself
|
|
232
287
|
|
|
233
288
|
When you already have a duration, log it without a block. This keeps the duration as structured data
|
|
@@ -247,16 +302,30 @@ The second argument to a measure call is a Hash of options:
|
|
|
247
302
|
- `:min_duration` [Float]: only log if the block takes longer than this many milliseconds. Default
|
|
248
303
|
`0.0` (always log).
|
|
249
304
|
- `:metric` [String]: notify metric subscribers with this metric name.
|
|
305
|
+
- `:dimensions` [Hash]: key/value labels that break the metric down, such as `{supplier: "acme"}`.
|
|
250
306
|
- `:payload` [Hash]: an optional payload to log with the entry.
|
|
251
307
|
- `:exception` [Exception]: an exception to log along with the duration.
|
|
252
308
|
- `:duration` [Float]: the duration in ms, used when no block is supplied (then it is mandatory; with
|
|
253
309
|
a block it is ignored).
|
|
254
310
|
- `:log_exception` [Symbol]: how to report an exception raised in the block. `:full` logs the class,
|
|
255
|
-
message, and backtrace
|
|
256
|
-
|
|
311
|
+
message, and backtrace of the exception and of each exception that caused it; `:partial` adds their
|
|
312
|
+
classes and messages to the log message, without backtraces; `:none` does not log it. Default
|
|
313
|
+
`:partial`. With `:partial`, a cause's message is left out when it is empty, is its class name, or
|
|
314
|
+
is already in the log message, as when the exception that wraps it copied it into its own:
|
|
315
|
+
|
|
316
|
+
```
|
|
317
|
+
Rendering -- Exception: ActionDispatch::Http::MimeNegotiation::InvalidType: "foo/bar" is not a valid MIME type -- Cause: Mime::Type::InvalidMimeType
|
|
318
|
+
```
|
|
257
319
|
- `:on_exception_level` [Symbol]: if an exception is raised, raise the log level to this level.
|
|
258
|
-
- `:silence` [Symbol]: the level to silence other log messages to within the block (current
|
|
320
|
+
- `:silence` [Symbol]: the level to silence other log messages to within the block (current fiber
|
|
259
321
|
only).
|
|
322
|
+
- `:call_site` [Thread::Backtrace::Location|SemanticLogger::CallSite|false]: where the entry was
|
|
323
|
+
logged, or `false` for none. See
|
|
324
|
+
[Recording where each entry was logged](config.html#recording-where-each-entry-was-logged).
|
|
325
|
+
|
|
326
|
+
An option with a value that is not valid for it, such as a non-numeric `:min_duration` or an unknown
|
|
327
|
+
level, is ignored and its default is used. Other keys in the Hash are not logged; pass the data to
|
|
328
|
+
log as `:payload`.
|
|
260
329
|
|
|
261
330
|
Putting several together:
|
|
262
331
|
|
|
@@ -291,13 +360,60 @@ SemanticLogger.tagged(user: "Jack", zip_code: 12345) do
|
|
|
291
360
|
end
|
|
292
361
|
~~~
|
|
293
362
|
|
|
294
|
-
Tags
|
|
295
|
-
[
|
|
296
|
-
|
|
363
|
+
Tags belong to the current fiber: each thread has its own, and so does each fiber of a fiber
|
|
364
|
+
scheduler such as [Async](https://github.com/socketry/async), which runs every request of a
|
|
365
|
+
[Falcon](https://github.com/socketry/falcon) server in its own fiber. So concurrent requests never
|
|
366
|
+
see each other's tags, and a thread, fiber, or Async task started inside the block does not have
|
|
367
|
+
them either. Hand them over explicitly, as shown next.
|
|
368
|
+
|
|
369
|
+
### Hand tags to another thread or fiber
|
|
370
|
+
|
|
371
|
+
`SemanticLogger.context` returns a frozen snapshot of the current tags, named tags, and
|
|
372
|
+
[silence](#silence-noisy-code) level. `SemanticLogger.with_context` applies it in another thread or
|
|
373
|
+
fiber for the duration of a block:
|
|
374
|
+
|
|
375
|
+
~~~ruby
|
|
376
|
+
SemanticLogger.tagged(request_id: request.uuid) do
|
|
377
|
+
context = SemanticLogger.context
|
|
378
|
+
|
|
379
|
+
Thread.new do
|
|
380
|
+
SemanticLogger.with_context(context) do
|
|
381
|
+
logger.info("Generating report") # carries request_id
|
|
382
|
+
end
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
~~~
|
|
386
|
+
|
|
387
|
+
The same works for an Async task or a thread pool:
|
|
388
|
+
|
|
389
|
+
~~~ruby
|
|
390
|
+
context = SemanticLogger.context
|
|
391
|
+
barrier = Async::Barrier.new
|
|
392
|
+
urls.each do |url|
|
|
393
|
+
barrier.async do
|
|
394
|
+
SemanticLogger.with_context(context) { fetch(url) }
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
barrier.wait
|
|
398
|
+
|
|
399
|
+
context = SemanticLogger.context
|
|
400
|
+
pool.post do
|
|
401
|
+
SemanticLogger.with_context(context) { process(item) }
|
|
402
|
+
end
|
|
403
|
+
~~~
|
|
404
|
+
|
|
405
|
+
Within the block, the thread or fiber has exactly the tags, named tags, and silence level of the
|
|
406
|
+
snapshot, in place of its own, which come back when the block exits. Add more tags inside the block
|
|
407
|
+
as usual. The snapshot does not carry levels set with a logger's `with_level`, which, like Ruby's own
|
|
408
|
+
`Logger#with_level`, apply only to the fiber that set them.
|
|
409
|
+
|
|
410
|
+
Tags are not handed over automatically because a long-lived thread, such as a thread pool's worker,
|
|
411
|
+
would otherwise keep the tags of whichever request happened to start it, and log them for every
|
|
412
|
+
later request it serves.
|
|
297
413
|
|
|
298
414
|
### Bind tags to one logger (child loggers)
|
|
299
415
|
|
|
300
|
-
The block form above scopes tags to the
|
|
416
|
+
The block form above scopes tags to the fiber. Sometimes it is more convenient to bind tags to a
|
|
301
417
|
single logger instance, for example when the logger belongs to an object with its own identity (an
|
|
302
418
|
ActiveRecord model or a background job).
|
|
303
419
|
|
|
@@ -347,11 +463,11 @@ The features above cover everyday logging. The rest of this guide covers less co
|
|
|
347
463
|
|
|
348
464
|
### Name your threads
|
|
349
465
|
|
|
350
|
-
Semantic Logger includes the thread name
|
|
351
|
-
|
|
466
|
+
Semantic Logger includes the thread name in every entry. A thread that has no name is identified by
|
|
467
|
+
its object id instead:
|
|
352
468
|
|
|
353
469
|
~~~
|
|
354
|
-
2013-11-07 16:25:14.279 I [35841:
|
|
470
|
+
2013-11-07 16:25:14.279 I [35841:16] (0.0ms) ExternalSupplier -- Calling external interface
|
|
355
471
|
~~~
|
|
356
472
|
|
|
357
473
|
Give a thread a readable name so it stands out in the logs:
|
|
@@ -364,15 +480,75 @@ Thread.current.name = "User calculation thread 32"
|
|
|
364
480
|
2013-11-07 16:26:02.744 I [35841:User calculation thread 32] (0.0ms) ExternalSupplier -- Calling external interface
|
|
365
481
|
~~~
|
|
366
482
|
|
|
367
|
-
Keep the name unique, otherwise concurrent threads are hard to tell apart.
|
|
368
|
-
|
|
483
|
+
Keep the name unique, otherwise concurrent threads are hard to tell apart. For example, number the
|
|
484
|
+
threads in a pool:
|
|
369
485
|
|
|
370
486
|
~~~ruby
|
|
371
|
-
|
|
487
|
+
workers = Array.new(5) do |i|
|
|
488
|
+
Thread.new do
|
|
489
|
+
Thread.current.name = "calc #{i + 1}"
|
|
490
|
+
# ...
|
|
491
|
+
end
|
|
492
|
+
end
|
|
372
493
|
~~~
|
|
373
494
|
|
|
374
|
-
|
|
375
|
-
|
|
495
|
+
This is Ruby's own `Thread#name=`, so the name must be a String, and Ruby raises for one that
|
|
496
|
+
contains a NUL byte or is not in an ASCII-compatible encoding. Ruby also gives the name to the
|
|
497
|
+
operating system, so it shows up in tools that list threads, such as `top -H`, debuggers, and
|
|
498
|
+
profilers. Log entries show the whole name, but Linux keeps only its first 15 bytes, so keep the
|
|
499
|
+
name short, or put the part that tells threads apart first. Semantic Logger keeps its own thread
|
|
500
|
+
names short for the same reason, naming each after its appender, such as `SL Appenders` and
|
|
501
|
+
`SL File`.
|
|
502
|
+
|
|
503
|
+
On JRuby, naming a thread also names the underlying JVM thread, which shows up when monitoring the
|
|
504
|
+
JVM with tools such as jconsole or jstack. JRuby names the main thread `main`, and a thread created
|
|
505
|
+
by Java code, such as a servlet container's request thread, is logged with its JVM thread name.
|
|
506
|
+
|
|
507
|
+
### Log from Ractors (experimental)
|
|
508
|
+
|
|
509
|
+
On Ruby 4.0 and later, code running in a [Ractor](https://docs.ruby-lang.org/en/4.0/Ractor.html)
|
|
510
|
+
logs as usual, and the entries are written by the appenders added in the main Ractor. Like Ruby's
|
|
511
|
+
own Ractor API, this is experimental, and may change in a minor release.
|
|
512
|
+
|
|
513
|
+
~~~ruby
|
|
514
|
+
SemanticLogger.add_appender(io: $stdout, formatter: :color)
|
|
515
|
+
|
|
516
|
+
context = SemanticLogger.tagged(job_id: 42) { SemanticLogger.context }
|
|
517
|
+
worker = Ractor.new(context, name: "worker") do |job_context|
|
|
518
|
+
SemanticLogger.with_context(job_context) do
|
|
519
|
+
logger = SemanticLogger["Importer"]
|
|
520
|
+
logger.info("Importing", rows: 1_000) # carries job_id: 42
|
|
521
|
+
end
|
|
522
|
+
end
|
|
523
|
+
worker.join
|
|
524
|
+
~~~
|
|
525
|
+
|
|
526
|
+
Each entry is sent to the main Ractor, where a thread hands it to the appenders, so a Ractor never
|
|
527
|
+
waits on an appender. The thread name of an entry starts with the Ractor's name, or its number, as in
|
|
528
|
+
`worker/12345` or `ractor-3/12345`.
|
|
529
|
+
|
|
530
|
+
Things to know:
|
|
531
|
+
|
|
532
|
+
- A new Ractor does not have the tags of the code that created it. Hand them over with
|
|
533
|
+
[`SemanticLogger.context`](#hand-tags-to-another-thread-or-fiber), as shown above.
|
|
534
|
+
- Add appenders, and change any setting, such as `SemanticLogger.default_level`, in the main Ractor.
|
|
535
|
+
Doing so in another Ractor raises `Ractor::IsolationError`.
|
|
536
|
+
- Each Ractor has its own loggers: a `Loggable` class has a logger of its own in each Ractor, and so
|
|
537
|
+
does a class with `SemanticLogger.cache_loggers` enabled. Setting a level on the main Ractor's logger
|
|
538
|
+
therefore does not change the level of a Ractor's logger; the global default level applies to both.
|
|
539
|
+
- `on_log` subscribers run for an entry logged in a Ractor only when they are shareable, such as a
|
|
540
|
+
module with a `call` method, or a block made with `Ractor.shareable_proc`.
|
|
541
|
+
- An entry is copied to the main Ractor. A value that Ruby cannot copy, such as a Proc in the payload,
|
|
542
|
+
is written as its `inspect`, and so is such a value held by a logged exception. The extra detail
|
|
543
|
+
that Ruby adds to some exceptions' messages, such as did_you_mean's "Did you mean?" suggestions, is
|
|
544
|
+
not written for an exception logged in a Ractor.
|
|
545
|
+
- `SemanticLogger.flush` in a Ractor waits until the main Ractor has written that Ractor's entries.
|
|
546
|
+
- The main Ractor accepts entries from other Ractors as fast as they are logged, so logging never
|
|
547
|
+
slows a Ractor down, however busy the appenders are. `max_queue_size` and `non_blocking` then apply
|
|
548
|
+
to the entries as usual, once the receiving thread hands them to the processor. In sync mode, too,
|
|
549
|
+
the receiving thread writes them.
|
|
550
|
+
- On Ruby 3.4 and earlier, logging from a Ractor other than the main one raises
|
|
551
|
+
`Ractor::IsolationError`, as it always has.
|
|
376
552
|
|
|
377
553
|
### Change one class's level at runtime
|
|
378
554
|
|
|
@@ -395,7 +571,7 @@ To change the global default level for every logger that has not been set explic
|
|
|
395
571
|
|
|
396
572
|
### Silence noisy code
|
|
397
573
|
|
|
398
|
-
`silence` raises the level within a block, on the current
|
|
574
|
+
`silence` raises the level within a block, on the current fiber only, to quiet a noisy section:
|
|
399
575
|
|
|
400
576
|
~~~ruby
|
|
401
577
|
# Within this block, log only :error and above
|
|
@@ -415,7 +591,8 @@ end
|
|
|
415
591
|
~~~
|
|
416
592
|
|
|
417
593
|
`silence` has no effect on loggers whose level was set explicitly (those that do not follow the global
|
|
418
|
-
default), and does not affect threads
|
|
594
|
+
default), and does not affect threads or fibers started inside the block, unless they are handed the
|
|
595
|
+
block's [context](#hand-tags-to-another-thread-or-fiber).
|
|
419
596
|
|
|
420
597
|
### Map a noisy gem's debug logs to trace
|
|
421
598
|
|