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.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -12
  3. data/Rakefile +15 -4
  4. data/docs/api.md +196 -19
  5. data/docs/appenders.md +579 -68
  6. data/docs/config.md +647 -38
  7. data/docs/index.md +31 -5
  8. data/docs/log.md +56 -17
  9. data/docs/metrics.md +144 -30
  10. data/docs/operations.md +339 -24
  11. data/docs/rails.md +396 -8
  12. data/docs/security.md +222 -27
  13. data/docs/testing.md +28 -3
  14. data/docs/upgrading.md +938 -7
  15. data/lib/semantic_logger/appender/async.rb +42 -11
  16. data/lib/semantic_logger/appender/bugsnag.rb +7 -2
  17. data/lib/semantic_logger/appender/cloudwatch_logs.rb +168 -33
  18. data/lib/semantic_logger/appender/elasticsearch_base.rb +221 -28
  19. data/lib/semantic_logger/appender/elasticsearch_http.rb +23 -6
  20. data/lib/semantic_logger/appender/file.rb +160 -37
  21. data/lib/semantic_logger/appender/graylog.rb +101 -11
  22. data/lib/semantic_logger/appender/honeybadger.rb +7 -1
  23. data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
  24. data/lib/semantic_logger/appender/http.rb +308 -46
  25. data/lib/semantic_logger/appender/io.rb +11 -6
  26. data/lib/semantic_logger/appender/kafka.rb +204 -44
  27. data/lib/semantic_logger/appender/loki.rb +55 -9
  28. data/lib/semantic_logger/appender/mongodb.rb +109 -5
  29. data/lib/semantic_logger/appender/new_relic.rb +3 -4
  30. data/lib/semantic_logger/appender/new_relic_logs.rb +35 -15
  31. data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
  32. data/lib/semantic_logger/appender/rabbitmq.rb +246 -17
  33. data/lib/semantic_logger/appender/sentry.rb +3 -4
  34. data/lib/semantic_logger/appender/sentry_ruby.rb +159 -18
  35. data/lib/semantic_logger/appender/splunk.rb +11 -2
  36. data/lib/semantic_logger/appender/splunk_http.rb +92 -1
  37. data/lib/semantic_logger/appender/syslog.rb +149 -32
  38. data/lib/semantic_logger/appender/tcp.rb +73 -98
  39. data/lib/semantic_logger/appender/text_output.rb +65 -0
  40. data/lib/semantic_logger/appender/udp.rb +140 -18
  41. data/lib/semantic_logger/appender/wrapper.rb +10 -7
  42. data/lib/semantic_logger/appender.rb +115 -10
  43. data/lib/semantic_logger/appenders.rb +145 -5
  44. data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
  45. data/lib/semantic_logger/base.rb +115 -81
  46. data/lib/semantic_logger/call_site.rb +235 -0
  47. data/lib/semantic_logger/context.rb +222 -0
  48. data/lib/semantic_logger/core_ext/process.rb +20 -5
  49. data/lib/semantic_logger/core_ext/warning.rb +70 -0
  50. data/lib/semantic_logger/deprecation.rb +139 -0
  51. data/lib/semantic_logger/exception_details.rb +51 -0
  52. data/lib/semantic_logger/formatters/base.rb +186 -20
  53. data/lib/semantic_logger/formatters/color.rb +18 -3
  54. data/lib/semantic_logger/formatters/default.rb +53 -8
  55. data/lib/semantic_logger/formatters/ecs.rb +9 -4
  56. data/lib/semantic_logger/formatters/json.rb +9 -0
  57. data/lib/semantic_logger/formatters/logfmt.rb +155 -21
  58. data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
  59. data/lib/semantic_logger/formatters/loki.rb +430 -38
  60. data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
  61. data/lib/semantic_logger/formatters/one_line.rb +101 -3
  62. data/lib/semantic_logger/formatters/open_telemetry.rb +191 -35
  63. data/lib/semantic_logger/formatters/pattern.rb +68 -12
  64. data/lib/semantic_logger/formatters/raw.rb +40 -11
  65. data/lib/semantic_logger/formatters/signalfx.rb +55 -15
  66. data/lib/semantic_logger/formatters/syslog.rb +114 -20
  67. data/lib/semantic_logger/formatters/syslog_cee.rb +38 -19
  68. data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
  69. data/lib/semantic_logger/formatters.rb +1 -0
  70. data/lib/semantic_logger/levels.rb +10 -1
  71. data/lib/semantic_logger/log.rb +384 -82
  72. data/lib/semantic_logger/loggable.rb +56 -73
  73. data/lib/semantic_logger/logger.rb +31 -5
  74. data/lib/semantic_logger/metric/new_relic.rb +33 -18
  75. data/lib/semantic_logger/metric/signalfx.rb +29 -18
  76. data/lib/semantic_logger/metric/statsd.rb +41 -15
  77. data/lib/semantic_logger/processor.rb +194 -5
  78. data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
  79. data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
  80. data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
  81. data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
  82. data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
  83. data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
  84. data/lib/semantic_logger/queue_processor/request.rb +93 -0
  85. data/lib/semantic_logger/queue_processor/throttled_counter.rb +89 -0
  86. data/lib/semantic_logger/queue_processor.rb +519 -170
  87. data/lib/semantic_logger/ractor_copy.rb +120 -0
  88. data/lib/semantic_logger/ractor_processor.rb +82 -0
  89. data/lib/semantic_logger/ractor_receiver.rb +282 -0
  90. data/lib/semantic_logger/ractor_support.rb +28 -0
  91. data/lib/semantic_logger/redactor.rb +310 -0
  92. data/lib/semantic_logger/semantic_logger.rb +328 -81
  93. data/lib/semantic_logger/subscriber.rb +250 -10
  94. data/lib/semantic_logger/sync_processor.rb +237 -26
  95. data/lib/semantic_logger/test/capture_log_events.rb +26 -5
  96. data/lib/semantic_logger/test/minitest.rb +2 -1
  97. data/lib/semantic_logger/test/rspec.rb +1 -1
  98. data/lib/semantic_logger/trap_safe.rb +113 -0
  99. data/lib/semantic_logger/utils.rb +482 -67
  100. data/lib/semantic_logger/version.rb +1 -1
  101. data/lib/semantic_logger.rb +36 -3
  102. metadata +41 -5
  103. data/lib/semantic_logger/core_ext/thread.rb +0 -30
data/docs/appenders.md CHANGED
@@ -1,9 +1,12 @@
1
1
  ---
2
2
  layout: default
3
+ title: Appenders
4
+ description: >-
5
+ Every destination Semantic Logger writes to and how to configure it: files
6
+ and streams, structured formats, network protocols, log aggregators, and
7
+ error monitors.
3
8
  ---
4
9
 
5
- ## Appenders
6
- {:.no_toc}
7
10
 
8
11
  **Contents**
9
12
 
@@ -37,6 +40,12 @@ SemanticLogger.add_appender(logger: Logger.new($stdout))
37
40
  SemanticLogger.add_appender(metric: :statsd, url: "udp://localhost:8125")
38
41
  ~~~
39
42
 
43
+ Pass one of these keywords to each call, and call `add_appender` once for each destination, see
44
+ [Logging to several destinations at once](#logging-to-several-destinations-at-once). Given more than
45
+ one, it adds only the first of `file_name:`, `io:`, `logger:`, `appender:`, and `metric:`, and
46
+ reports a [deprecation warning](upgrading.html#seeing-deprecation-warnings); v6 raises an
47
+ `ArgumentError`.
48
+
40
49
  In a Rails app using [rails_semantic_logger](rails.html), put `add_appender` calls in an initializer
41
50
  such as `config/initializers/semantic_logger.rb`. Otherwise add them wherever you configure Semantic
42
51
  Logger when the application boots.
@@ -48,10 +57,11 @@ destination can keep a different subset of the logs in a different format:
48
57
 
49
58
  | Option | Description |
50
59
  |--------|-------------|
51
- | `level` | Only write entries at this level or higher to this appender. Defaults to `SemanticLogger.default_level`. |
52
- | `formatter` | How to format the output, for example `:default`, `:color`, or `:json`. See [Custom formatters](config.html#custom-formatters). |
60
+ | `level` | Only write entries at this level or higher to this appender; for what it defaults to, and how it works with each logger's own level, see [Per-appender settings](config.html#per-appender-settings). |
61
+ | `formatter` | How to format the output, for example `:default`, `:color`, or `:json`. A [block given to `add_appender`](config.html#formatter-as-a-block-or-proc) takes its place. See [Custom formatters](config.html#custom-formatters). |
53
62
  | `filter` | A `Regexp` or `Proc` selecting which entries this appender accepts. See [Filtering](config.html#filtering). |
54
63
  | `application`, `environment`, `host` | Override the global values for this appender only. |
64
+ | `metrics` | Whether to also write metric-only entries, which have a metric but no message and no exception. Defaults to `false`, except for `:kafka`, `:rabbitmq`, and `:udp`, which default to `true` until v6, and warn when it is not supplied. See [Metrics in log appenders](metrics.html#metrics-in-log-appenders). |
55
65
 
56
66
  For example, write only warnings and above to a file, formatted as JSON:
57
67
 
@@ -83,7 +93,7 @@ time the appender is used, and is never a hard dependency of Semantic Logger its
83
93
  | [HTTP(S)](#https) | `appender: :http` | |
84
94
  | [TCP (+SSL)](#tcp-appender-ssl) | `appender: :tcp` | `net_tcp_client` |
85
95
  | [UDP](#udp-appender) | `appender: :udp` | |
86
- | [Syslog](#syslog) | `appender: :syslog` | `syslog_protocol`, `net_tcp_client` (remote) |
96
+ | [Syslog](#syslog) | `appender: :syslog` | `syslog` (local, Ruby 3.4+), `syslog_protocol` (remote), `net_tcp_client` (TCP) |
87
97
 
88
98
  **Centralized logging and log aggregators**
89
99
 
@@ -93,7 +103,7 @@ time the appender is used, and is never a hard dependency of Semantic Logger its
93
103
  | [OpenSearch](#opensearch) | `appender: :opensearch` | `opensearch-ruby` |
94
104
  | [Graylog](#graylog) | `appender: :graylog` | `gelf` |
95
105
  | [Splunk over HTTP](#splunk-http) | `appender: :splunk_http` | |
96
- | [Splunk SDK](#splunk-sdk) | `appender: :splunk` | `splunk-sdk-ruby` |
106
+ | [Splunk SDK](#splunk-sdk) (deprecated) | `appender: :splunk` | `splunk-sdk-ruby` |
97
107
  | [Grafana Loki](#grafana-loki) | `appender: :loki` | |
98
108
  | [CloudWatch Logs](#cloudwatch-logs) | `appender: :cloudwatch_logs` | `aws-sdk-cloudwatchlogs` |
99
109
  | [OpenTelemetry](#opentelemetry) | `appender: :open_telemetry` | `opentelemetry-logs-sdk` |
@@ -158,6 +168,14 @@ file is created and to an existing log file:
158
168
  SemanticLogger.add_appender(file_name: "production.log", permissions: 0o640)
159
169
  ~~~
160
170
 
171
+ With `permissions:`, the log file must be a regular file owned by the process, with a single hard
172
+ link, that the file name names directly rather than through a symbolic link. Nothing is written to
173
+ any other file, such as a log file linked to `/dev/stdout`, nor when the file cannot be opened:
174
+ `add_appender` raises `IOError` giving the reason, and a file that fails the check when it is
175
+ reopened later, for example on a rotation, is reported and not written to until it is fixed. To log
176
+ to standard output, use `io: $stdout` instead. See
177
+ [Log file permissions](security.html#log-file-permissions).
178
+
161
179
  ### IO Streams
162
180
 
163
181
  Log to any IO stream instance, such as `$stdout` or `$stderr`:
@@ -220,6 +238,7 @@ The file, IO, and HTTP appenders can emit machine-readable output by choosing a
220
238
  "thread": "Thread name or id",
221
239
  "file": "filename",
222
240
  "line": "line number",
241
+ "method": "Method that logged it, such as Order#submit",
223
242
 
224
243
  "level": "trace|debug|info|warn|error|fatal",
225
244
  "level_index": "0|1|2|3|4|5",
@@ -230,6 +249,7 @@ The file, IO, and HTTP appenders can emit machine-readable output by choosing a
230
249
  "duration_ms": "Duration in milliseconds",
231
250
  "metric": "Name of the metric",
232
251
  "metric_amount": "Size of the metric, usually 1",
252
+ "dimensions": {"region": "The metric's dimensions, when it has any"},
233
253
 
234
254
  "named_tags": {
235
255
  "tag1": "any named tags will be inside this named_tags tag",
@@ -259,6 +279,15 @@ Notes:
259
279
  * The layout above is formatted for readability. The real output is a single line terminated by one
260
280
  newline, with no embedded newlines.
261
281
  * A field with a `nil` value is excluded from the output.
282
+ * An exception, and each exception that caused it, is named as Ruby names its class in
283
+ `Exception#inspect`, as every formatter names it: by the constant the class was assigned to, even
284
+ when the class overrides `self.name`, or for an anonymous class by the class as Ruby writes it,
285
+ such as `#<Class:0x000...>`. Its `message` is always a String: when the exception's `#message`
286
+ raises, such as one that reads a record that is nil, the message it was raised with is written
287
+ instead.
288
+ * To also log the detail Ruby adds to an exception's message, such as did_you_mean suggestions, as
289
+ `exception.details`, enable `log_exception_details`: `formatter: {json: {log_exception_details: true}}`.
290
+ See [Exception details](config.html#exception-details).
262
291
 
263
292
  ### Fluentd
264
293
 
@@ -274,7 +303,7 @@ Differences from `:json`:
274
303
  * `level` / `level_index` become `severity` / `severity_index`.
275
304
  * `host` is excluded by default (under Fluentd it is usually the not-very-useful container id). Pass
276
305
  `log_host: true` to include it.
277
- * The process fields `pid`, `thread`, `file`, and `line` are excluded by default. Pass
306
+ * The process fields `pid`, `thread`, `file`, `line`, and `method` are excluded by default. Pass
278
307
  `need_process_info: true` to include them.
279
308
 
280
309
  Construct the formatter explicitly to override these defaults:
@@ -305,6 +334,7 @@ Semantic Logger fields map to ECS as follows:
305
334
  | `level` | `log.level` |
306
335
  | `name` | `log.logger` |
307
336
  | `file` / `line` | `log.origin.file.name` / `log.origin.file.line` |
337
+ | `method` | `log.origin.function` |
308
338
  | `message` | `message` |
309
339
  | `thread` | `process.thread.name` |
310
340
  | `pid` | `process.pid` |
@@ -315,10 +345,10 @@ Semantic Logger fields map to ECS as follows:
315
345
  | `duration` | `event.duration` (nanoseconds) |
316
346
  | `tags` | `tags` |
317
347
  | `named_tags` | `labels.*` |
318
- | `payload`, `metric`, `metric_amount` | nested under a custom namespace (see below) |
348
+ | `payload`, `metric`, `metric_amount`, `dimensions` | nested under a custom namespace (see below) |
319
349
 
320
350
  ECS reserves the top-level field names it defines, so Semantic Logger data with no native ECS home
321
- (`payload`, `metric`, and `metric_amount`) is nested under a custom top-level namespace,
351
+ (`payload`, `metric`, `metric_amount`, and `dimensions`) is nested under a custom top-level namespace,
322
352
  `semantic_logger` by default. A proper-noun namespace is
323
353
  [the approach ECS recommends](https://www.elastic.co/docs/reference/ecs/ecs-custom-fields-in-ecs) for
324
354
  custom fields, since it never collides with a current or future ECS field. Rename it with
@@ -351,13 +381,30 @@ timestamp="2024-07-20T08:32:05.375276Z" level="info" name="MyClass" message="Hel
351
381
 
352
382
  Notes:
353
383
 
354
- * The timestamp is ISO 8601. All values are quoted and escaped, so a newline in the data cannot
355
- forge or split a record.
356
- * Named tags and payload fields are merged into the top-level `key=value` pairs. On a name
357
- conflict the payload wins.
384
+ * The timestamp is ISO 8601, under the key `timestamp` unless the formatter's `time_key` is set
385
+ (`formatter: {logfmt: {time_key: :ts}}`). All values are quoted and escaped, so a newline in the
386
+ data cannot forge or split a record.
387
+ * Keys (from tags, named tags, and the payload) cannot be quoted in logfmt, so spaces, control
388
+ characters (including the Unicode line and paragraph separators), invisible format characters
389
+ (such as a zero width space), `=`, and `"` in a key are replaced with `_`, and invalid UTF-8 is
390
+ dropped. For example the tag `second breakfast` is emitted as `second_breakfast=true`.
391
+ * Named tags and payload fields are merged into the top-level `key=value` pairs. A key named like
392
+ one of the entry's own fields (the time key, `level`, `name`, `message`, `duration`,
393
+ `duration_ms`, `tag`, or an exception field) is emitted with its source as a prefix, so that
394
+ logged data cannot replace those fields: `payload.level="fatal"`, `named_tags.message="..."`, or
395
+ `tags.message=true`. A key that starts with `tags.`, `named_tags.`, or `payload.` is prefixed
396
+ too, as in `payload.named_tags.level`, so that it cannot pass for another source's field.
397
+ * Other keys that are the same once encoded, including keys that only differ by type (such as
398
+ `:user` and `"user"`), are emitted once, with the value from the later source: the payload wins
399
+ over named tags, and named tags over tags.
358
400
  * Unnamed tags are emitted as keys with a `true` value.
401
+ * A nested value, such as a Hash or an Array in the payload, is emitted as its JSON text, quoted:
402
+ `user="{\"id\":7}"`.
359
403
  * `tag="success"` is emitted on every entry, becoming `tag="exception"` when the entry carries an
360
- exception (the exception class, message, and backtrace are then included as fields).
404
+ exception, which is then included as the fields `exception_name`, `exception_message`,
405
+ `stack_trace`, and, when the exception has a cause, `cause`. With `log_exception_details: true`
406
+ (off by default), the detail Ruby adds to the exception's message is included as
407
+ `exception_details`; see [Exception details](config.html#exception-details).
361
408
 
362
409
  ## Network protocols
363
410
 
@@ -388,6 +435,31 @@ end
388
435
  SemanticLogger.add_appender(appender: :http, url: "https://localhost:8088/path", formatter: formatter)
389
436
  ~~~
390
437
 
438
+ #### Query parameters
439
+
440
+ Some log servers are configured through the query string of the ingestion url rather than through
441
+ headers or the body. A query string on `url:` is kept and sent with every request that submits log
442
+ data, for example when pointing at
443
+ [VictoriaLogs](https://docs.victoriametrics.com/victorialogs/data-ingestion/):
444
+
445
+ ~~~ruby
446
+ SemanticLogger.add_appender(
447
+ appender: :http,
448
+ url: "http://localhost:9428/insert/jsonline?_msg_field=message&_time_field=timestamp"
449
+ )
450
+ ~~~
451
+
452
+ The query is applied after the request path, so it also works for the appenders built on top of the
453
+ HTTP appender (`:elasticsearch_http`, `:splunk_http`, `:loki`, and the `:signalfx` metrics appender),
454
+ each of which appends its own path to the url:
455
+
456
+ ~~~ruby
457
+ SemanticLogger.add_appender(
458
+ appender: :elasticsearch_http,
459
+ url: "http://localhost:9428/insert/elasticsearch?_msg_field=message&_time_field=timestamp"
460
+ )
461
+ ~~~
462
+
391
463
  #### Batching
392
464
 
393
465
  By default each entry is sent in its own HTTP request. To send multiple entries in one request as a
@@ -399,6 +471,15 @@ one document per element, such as the
399
471
  SemanticLogger.add_appender(appender: :http, url: "http://localhost:8088/path", batch: true)
400
472
  ~~~
401
473
 
474
+ The formatter formats the whole batch with its `#batch(logs, logger)`, as the default `:json`
475
+ formatter does. `add_appender` raises an `ArgumentError` for a formatter that cannot, such as a
476
+ [formatter block](config.html#formatter-as-a-block-or-proc), given with `batch: true`.
477
+
478
+ `:elasticsearch_http` cannot be batched, since Elasticsearch indexes a batch of entries only through
479
+ its bulk API, and rejects a JSON array: `add_appender` raises an `ArgumentError` for it with
480
+ `batch: true`. To send the entries in batches, use the [Elasticsearch appender](#elasticsearch),
481
+ which sends them in bulk requests.
482
+
402
483
  With batching the appender runs on its own thread and flushes once `batch_size` entries have
403
484
  accumulated (default 300) or `batch_seconds` have elapsed (default 5), whichever comes first:
404
485
 
@@ -412,6 +493,79 @@ SemanticLogger.add_appender(
412
493
  )
413
494
  ~~~
414
495
 
496
+ #### When the server does not accept a request
497
+
498
+ An entry is written once the server answers its request with a 2xx status. The HTTP appender, and
499
+ those built on it (`:elasticsearch_http`, `:splunk_http`, `:loki`, and the `:signalfx` metrics
500
+ appender), handle any other answer by its status:
501
+
502
+ * A 4xx other than 408 and 429, such as `400 Bad Request` or `401 Unauthorized`, rejects the request
503
+ itself, so sending it again cannot succeed. With `async: true` or `batch: true`, its entries are
504
+ discarded straight away. When the server rejects a batch, which is sent in one request, for the
505
+ entries it holds, with `400 Bad Request`, `413 Content Too Large`, or `422 Unprocessable Content`,
506
+ the batch is first split up and sent again in parts, so that only the entries in the parts the
507
+ server rejects are discarded. Any other such 4xx, such as `401 Unauthorized`, `403 Forbidden`, or
508
+ `404 Not Found`, rejects the request whatever it holds, so the whole batch is discarded at once.
509
+ * `408 Request Timeout`, `429 Too Many Requests`, or `503 Service Unavailable` asks to send the
510
+ request again later, and the server did not store the entries. With `async: true` or
511
+ `batch: true`, the worker thread [backs off](operations.html#retry-a-failing-appender), then sends
512
+ the same entry or batch again, so that it is written once the server is back, ahead of the entries
513
+ logged meanwhile, which wait on the queue. Once the server has answered it in this way after it
514
+ was sent again 3 times, it is discarded, and the worker thread carries on with the next one once
515
+ it has backed off, so that a request the server keeps turning away, such as one it answers with a
516
+ `429` that never clears, does not hold up the others.
517
+ * Any other status, such as `500 Internal Server Error`, `502 Bad Gateway`, `504 Gateway Timeout`,
518
+ or a redirect, is a problem with the destination too, but the entry or batch is not sent again: a
519
+ `500` often comes again for the same request, and a proxy can answer a `502` or `504` once the
520
+ server behind it has stored it. With `async: true` or `batch: true`, it is discarded straight
521
+ away, and the worker thread backs off before sending the next one.
522
+
523
+ A request that the appender could not send, since it could not connect to the server (the name does
524
+ not resolve, the server cannot be reached, or it refuses the connection or does not accept it within
525
+ `open_timeout`), is sent again in the same way as a `503`, but without counting towards the 3 times:
526
+ the server has not answered it, so an outage loses no entries until the worker thread gives up. A
527
+ request that failed once it was sent, such as one whose answer did not come within `read_timeout`,
528
+ or whose connection was reset, is discarded instead, after the same back-off, since the server may
529
+ have stored its entries. While a flush is waiting, such as the one at exit, a request that fails is
530
+ discarded rather than sent again, and so are the entries queued after it, ahead of the flush,
531
+ without being sent; see [Retry a failing appender](operations.html#retry-a-failing-appender).
532
+
533
+ The worker thread stops for good after `async_max_retries` (default `100`) failures in a row,
534
+ discarding the entry or batch it was sending; pass `async_max_retries: -1` to keep trying through a
535
+ long outage.
536
+
537
+ The discarded entries are counted as `failed` in `SemanticLogger.stats`, and reported to the internal
538
+ logger with the status code, the url without its credentials, and the start of the response body.
539
+ Without `async:` or `batch:`, each request that fails is reported in the same way, and while the
540
+ process exits, once a request fails other than by the server rejecting it (a 4xx status other than
541
+ 408 and 429), the appender is given no more entries for the rest of the flush at exit, rather than
542
+ have the flush at exit wait for each one to fail in turn; see
543
+ [Retry a failing appender](operations.html#retry-a-failing-appender).
544
+
545
+ While the worker thread backs off, the entries logged meanwhile wait on the appender's own queue, as
546
+ they already did while the server could not be reached. Once that queue is full (`max_queue_size`,
547
+ default `10,000`), the thread that writes to every appender waits for room in it, so the other
548
+ appenders stop receiving entries as well, and once the main queue is full too, so does every logging
549
+ call, until the server accepts requests again. To keep logging while the server is unavailable, add
550
+ the appender with `non_blocking: true` as well, so that once its queue is full, its entries are
551
+ dropped and counted instead (see
552
+ [Drop messages instead of blocking](operations.html#drop-messages-instead-of-blocking)):
553
+
554
+ ~~~ruby
555
+ SemanticLogger.add_appender(
556
+ appender: :http,
557
+ url: "http://localhost:8088/path",
558
+ batch: true,
559
+ non_blocking: true
560
+ )
561
+ ~~~
562
+
563
+ When the server rejects every request with a `401`, `403`, or `404`, for example because the
564
+ password or the path is wrong, each batch is discarded with its first request, and reported once. A
565
+ server that answers `400` for a problem with its own configuration rejects every part of each batch
566
+ too, which takes about two requests for each entry in the batch. The `:splunk_http` appender tells
567
+ those apart for Splunk's HTTP Event Collector, see [Splunk HTTP](#splunk-http).
568
+
415
569
  ### TCP Appender (+SSL)
416
570
 
417
571
  The TCP appender sends JSON or other formatted messages to services that accept log messages over TCP,
@@ -424,11 +578,12 @@ SemanticLogger.add_appender(appender: :tcp, server: "localhost:8088")
424
578
  # TCP with SSL:
425
579
  SemanticLogger.add_appender(appender: :tcp, server: "localhost:8088", ssl: true)
426
580
 
427
- # With self-signed certificates, or to disable server certificate verification:
581
+ # With a self-signed certificate, or one from a private certificate authority, trust that
582
+ # certificate instead of disabling verification:
428
583
  SemanticLogger.add_appender(
429
584
  appender: :tcp,
430
585
  server: "localhost:8088",
431
- ssl: {verify_mode: OpenSSL::SSL::VERIFY_NONE}
586
+ ssl: {ca_file: "/path/to/ca.pem"}
432
587
  )
433
588
  ~~~
434
589
 
@@ -444,13 +599,27 @@ end
444
599
  SemanticLogger.add_appender(appender: :tcp, server: "localhost:8088", formatter: formatter)
445
600
  ~~~
446
601
 
447
- See [Net::TCPClient](https://github.com/reidmorrison/net_tcp_client) for the remaining connection
448
- options.
602
+ The `server:` is `"host:port"`. [Net::TCPClient](https://github.com/reidmorrison/net_tcp_client)
603
+ connects over IPv4 only, so give an IPv4 address, or a host name that has one. Pass several servers
604
+ as `servers:` to try the next when one cannot be connected to. Every host name is resolved before
605
+ the first of them is connected to, so a name that does not resolve fails at once, without trying the
606
+ others or retrying.
607
+
608
+ Net::TCPClient retries only after a network error, such as a refused, reset, or timed-out
609
+ connection: it connects again up to `connect_retry_count:` times (default 10), and writes an entry
610
+ again on a new connection up to `retry_count:` times (default 3). A write timeout, or an SSL error
611
+ such as a certificate that does not verify, fails without a retry. The appender passes these and its
612
+ other connection options, such as `connect_timeout:`, `write_timeout:`, and `ssl:`, to
613
+ `Net::TCPClient.new`, which raises `ArgumentError` for a keyword it does not take; see
614
+ [`Net::TCPClient#initialize`](https://github.com/reidmorrison/net_tcp_client/blob/master/lib/net/tcp_client/tcp_client.rb)
615
+ for them all and their defaults.
449
616
 
450
617
  The TCP and UDP appenders separate records with a newline, so they default to the JSON formatter,
451
618
  which escapes embedded newlines and is safe with untrusted data. If you switch to a text formatter
452
- such as `:default` or `:color`, enable `escape_control_chars` so a newline in the data cannot forge or
453
- split a record:
619
+ such as `:default` or `:color`, enable `escape_control_chars` so a newline in the logged data cannot
620
+ forge a record. Those formatters still write an entry with an exception over several lines (the
621
+ exception, then its backtrace), so a receiver that frames records by newline splits such an entry;
622
+ use the JSON formatter or the `:syslog` formatter when every record must be a single line:
454
623
 
455
624
  ~~~ruby
456
625
  SemanticLogger.add_appender(
@@ -465,9 +634,25 @@ SemanticLogger.add_appender(
465
634
  The UDP appender sends JSON or other formatted messages over UDP:
466
635
 
467
636
  ~~~ruby
468
- SemanticLogger.add_appender(appender: :udp, server: "localhost:8088")
637
+ SemanticLogger.add_appender(appender: :udp, server: "localhost:8088", metrics: false)
469
638
  ~~~
470
639
 
640
+ The `server:` is `"host:port"`. Write an IPv6 address in brackets, as a URL does, such as
641
+ `server: "[::1]:8088"`. A host name with an IPv4 address is sent to over IPv4, even when the
642
+ resolver returns its IPv6 address first, as it does for `localhost` on macOS. It is sent to over
643
+ IPv6 when it has only IPv6 addresses, or when the socket cannot connect over IPv4, such as on a
644
+ machine with no IPv4 route. Its IPv6 addresses are looked up only when the resolver answers that it
645
+ has no IPv4 address, or when the socket cannot connect to any of them. When looking up its IPv4
646
+ addresses fails for another reason, such as a resolver that does not answer in time, adding or
647
+ reopening the appender raises that `SocketError`. On Linux with glibc, that includes a name whose
648
+ only addresses are IPv6 ones in `/etc/hosts`, while DNS cannot be reached: its IPv4 lookup then
649
+ fails, rather than finding no address. On Ruby 3.2 and JRuby, whose `SocketError` does not say why
650
+ a lookup failed, the IPv6 addresses are looked up after any failure.
651
+
652
+ Pass `metrics: true` to also send metric-only entries, which have a metric but no message and no
653
+ exception. Without `metrics:`, it sends them and reports a
654
+ [deprecation warning](upgrading.html#seeing-deprecation-warnings), since v6 leaves them out by default.
655
+
471
656
  Customize the message with a formatter:
472
657
 
473
658
  ~~~ruby
@@ -477,12 +662,13 @@ formatter = Proc.new do |log, logger|
477
662
  h.to_json
478
663
  end
479
664
 
480
- SemanticLogger.add_appender(appender: :udp, server: "localhost:8088", formatter: formatter)
665
+ SemanticLogger.add_appender(appender: :udp, server: "localhost:8088", formatter: formatter, metrics: false)
481
666
  ~~~
482
667
 
483
668
  ### Syslog
484
669
 
485
- Log to a local Syslog daemon:
670
+ Log to a local Syslog daemon. Since Ruby 3.4 `syslog` is a bundled gem, so add `gem "syslog"` to the
671
+ Gemfile:
486
672
 
487
673
  ~~~ruby
488
674
  SemanticLogger.add_appender(appender: :syslog)
@@ -495,12 +681,39 @@ The default packet size is 1024 bytes:
495
681
  SemanticLogger.add_appender(appender: :syslog, url: "tcp://myloghost:514", max_size: 2048)
496
682
  ~~~
497
683
 
684
+ `max_size` also applies when `formatter:` names the `:syslog` or `:syslog_cee` formatter. A
685
+ formatter passed as an instance uses its own `max_size`, which also defaults to 1024 bytes:
686
+
687
+ ~~~ruby
688
+ SemanticLogger.add_appender(appender: :syslog, url: "udp://myloghost:514", formatter: :syslog_cee, max_size: 2048)
689
+ ~~~
690
+
498
691
  Or over UDP (the `syslog_protocol` gem is required):
499
692
 
500
693
  ~~~ruby
501
694
  SemanticLogger.add_appender(appender: :syslog, url: "udp://myloghost:514")
502
695
  ~~~
503
696
 
697
+ Set the facility with one of Ruby's `::Syslog::LOG_*` facility constants (the default is
698
+ `::Syslog::LOG_USER`). For remote syslog without the `syslog` gem, the same constants are available
699
+ from `SemanticLogger::Formatters::Syslog`, for example `SemanticLogger::Formatters::Syslog::LOG_LOCAL0`:
700
+
701
+ ~~~ruby
702
+ SemanticLogger.add_appender(appender: :syslog, url: "udp://myloghost:514", facility: ::Syslog::LOG_LOCAL0)
703
+ ~~~
704
+
705
+ The `facility:`, `level_map:`, and `max_size:` options also apply when `formatter:` names the `:syslog` or
706
+ `:syslog_cee` formatter, for example `formatter: :syslog_cee`. Options given to the formatter itself,
707
+ as in `formatter: {syslog: {facility: ::Syslog::LOG_LOCAL1}}`, take precedence. A formatter passed
708
+ as an instance keeps its own settings.
709
+
710
+ Over TCP or UDP, each record's header holds the host name and the application name, as its tag. The
711
+ `syslog_protocol` gem that builds the record allows only printable ASCII characters other than the
712
+ space in either, and up to 32 in the tag, so any other character is left out, and the application
713
+ name is cut to 32 characters:
714
+ an application named `"Café Orders"` is written as `CafOrders`. A name with no character left is
715
+ written as `localhost` or `SemanticLogger`.
716
+
504
717
  Add a filter to exclude noisy entries such as health checks:
505
718
 
506
719
  ~~~ruby
@@ -512,8 +725,10 @@ SemanticLogger.add_appender(
512
725
  ~~~
513
726
 
514
727
  Syslog frames each record, so embedded newlines or other control characters in untrusted data could
515
- forge or split records. The syslog formatters therefore escape control characters by default. To pass
516
- them through unchanged:
728
+ forge or split records. The syslog formatters therefore escape control characters by default. The
729
+ `:syslog` formatter also escapes the newlines between an exception and its backtrace lines (written
730
+ as `\n`), so that an entry with an exception is a single line. To pass control characters through
731
+ unchanged, which also restores the multi-line exception layout:
517
732
 
518
733
  ~~~ruby
519
734
  SemanticLogger.add_appender(
@@ -530,26 +745,51 @@ Note: `:trace` level messages are mapped to `:debug`.
530
745
  ### Elasticsearch
531
746
 
532
747
  Forward all log entries to Elasticsearch (requires the `elasticsearch` gem). By default entries are
533
- written to a daily index named `semantic_logger-YYYY.MM.DD`; override it with `index:`:
748
+ written to a daily index named `semantic_logger-YYYY.MM.DD`. `index:` sets the name before the date,
749
+ and `date_pattern:` the date, such as `"%Y.%m"` for a monthly index:
750
+
751
+ ~~~ruby
752
+ SemanticLogger.add_appender(
753
+ appender: :elasticsearch,
754
+ url: "http://localhost:9200",
755
+ index: "my-index"
756
+ )
757
+ ~~~
758
+
759
+ With `data_stream: true`, entries are written to the data stream that `index:` names, without a
760
+ date, and the default formatter writes the time under `@timestamp`, which a data stream requires.
761
+ Elasticsearch creates the data stream with the first entry when an index template that enables data
762
+ streams matches its name; otherwise it creates an ordinary index of that name:
534
763
 
535
764
  ~~~ruby
536
765
  SemanticLogger.add_appender(
537
766
  appender: :elasticsearch,
538
767
  url: "http://localhost:9200",
539
- index: "my-index",
768
+ index: "my-logs",
540
769
  data_stream: true
541
770
  )
542
771
  ~~~
543
772
 
773
+ The appender sends its entries in bulk requests, a batch at a time by default. When Elasticsearch
774
+ indexes only some of the entries of a request, the entries it indexed are not sent again. It rejects
775
+ an entry with status 429 when its write queue is full, so those entries alone are sent again, up to 3
776
+ times, after waiting 0.2, 0.4, then 0.8 seconds. That holds up the thread that writes the entries for
777
+ waits of at most 1.4 seconds for a request, plus up to 3 more bulk requests. It is the appender's own
778
+ thread when it writes in batches, as it does by default, or with `async: true`. With `batch: false`,
779
+ and without `async: true`, it is the thread that writes to every appender, so the other appenders
780
+ wait too; with `SemanticLogger.sync!`, that is the thread that logged, and every other thread that
781
+ logs waits for it. The entries still not indexed are discarded, and reported to the internal logger.
782
+
544
783
  For an end-to-end walkthrough with Kibana, see [Centralized Logging](operations.html#centralized-logging).
545
784
 
546
785
  ### OpenSearch
547
786
 
548
787
  Forward all log entries to OpenSearch, for example AWS OpenSearch (requires the `opensearch-ruby`
549
788
  gem). OpenSearch is a fork of Elasticsearch and uses the same bulk indexing API, so this appender
550
- accepts the same options as [Elasticsearch](#elasticsearch). Use it instead of the Elasticsearch
551
- appender when talking to an OpenSearch server, since recent `elasticsearch` gems reject
552
- non-Elasticsearch servers with an `Elasticsearch::UnsupportedProductError`:
789
+ accepts the same options as [Elasticsearch](#elasticsearch), and sends again the entries rejected
790
+ with status 429 in the same way. Use it instead of the Elasticsearch appender when talking to an
791
+ OpenSearch server, since recent `elasticsearch` gems reject non-Elasticsearch servers with an
792
+ `Elasticsearch::UnsupportedProductError`:
553
793
 
554
794
  ~~~ruby
555
795
  SemanticLogger.add_appender(
@@ -597,30 +837,53 @@ For HTTPS, change the URL scheme to `https`. Once entries have been sent, open t
597
837
  interface, select Search, limit to the new host (`host=hostname`), switch to table view, and select
598
838
  the interesting columns: `host`, `duration`, `name`, `level`, `message`.
599
839
 
600
- **Performance:** against a local Splunk instance, the HTTP collector handled only about 30 entries per
601
- second. For much higher throughput, write to Splunk with the [TCP appender](#tcp-appender-ssl)
602
- instead, which reached about 1,400 entries per second (1,200 with SSL).
603
-
604
- ### Splunk SDK
605
-
606
- `appender: :splunk` submits entries through the official `splunk-sdk-ruby` gem to the Splunk
607
- management port (8089 by default). Authenticate with either a username and password, or a
608
- pre-authenticated `token:`:
840
+ To send many entries in each request, add it with `batch: true`. Each request then holds the entries
841
+ of a batch as event objects stacked one after the other, each one what the appender sends for that
842
+ entry on its own, the batch format of Splunk's
843
+ [Format events for HTTP Event Collector](https://help.splunk.com/en/splunk-enterprise/get-started/get-data-in/10.6/get-data-with-http-event-collector/format-events-for-http-event-collector):
609
844
 
610
845
  ~~~ruby
611
846
  SemanticLogger.add_appender(
612
- appender: :splunk,
613
- username: "username",
614
- password: "password",
615
- host: "localhost",
616
- port: 8089,
617
- scheme: :https,
618
- index: "main"
847
+ appender: :splunk_http,
848
+ url: "http://localhost:8088/services/collector/event",
849
+ token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
850
+ batch: true
619
851
  )
620
852
  ~~~
621
853
 
622
- For most deployments the [HTTP Event Collector](#splunk-http) or the [TCP appender](#tcp-appender-ssl)
623
- is the simpler choice; use the SDK appender when the management API is the only ingress available.
854
+ A batch needs a formatter that formats each entry as exactly one event object, with its `event`
855
+ field, as the default formatter does. The collector names an event it rejects by its position in the
856
+ request, which the appender takes as the position of the entry in the batch. So do not add it with
857
+ `batch: true` and a formatter such as `:json`, whose objects the collector rejects, or one that
858
+ formats an entry as more than one event.
859
+
860
+ The HTTP Event Collector answers `400` both for the entries a request holds and for its own
861
+ configuration, and says which with the `code` and `text` in its response. When it rejects a request
862
+ for its configuration (an incorrect index, a missing or invalid data channel, acknowledgement
863
+ disabled, query string authorization not enabled, or an invalid or disabled token), a batch is
864
+ discarded at once and reported once to the internal logger with that text, such as
865
+ `"Incorrect index"`, rather than split up to find the entries at fault. When it rejects a batch at
866
+ one of its entries, such as with `"Invalid data format"`, it says which one, and has already indexed
867
+ those before it: that entry is discarded, and only the entries after it are sent again, so that none
868
+ is indexed twice. A `400` that does not say which entry, such as one from a proxy, has the batch split
869
+ up, as for any [request rejected for its entries](#when-the-server-does-not-accept-a-request). See
870
+ Splunk's
871
+ [HTTP Event Collector error codes](https://help.splunk.com/en/splunk-enterprise/get-started/get-data-in/10.4/get-data-with-http-event-collector/troubleshoot-http-event-collector).
872
+
873
+ **Performance:** against a local Splunk instance, the HTTP collector handled only about 30 entries per
874
+ second, one entry for each request. To send more, add it with `batch: true`, or write to Splunk with
875
+ the [TCP appender](#tcp-appender-ssl) instead, which reached about 1,400 entries per second (1,200
876
+ with SSL).
877
+
878
+ ### Splunk SDK
879
+
880
+ > **Deprecated:** `appender: :splunk` is deprecated and will be removed in v6. Use the
881
+ > [HTTP Event Collector](#splunk-http) (`appender: :splunk_http`) or the
882
+ > [TCP appender](#tcp-appender-ssl) instead.
883
+
884
+ `appender: :splunk` submits entries through the `splunk-sdk-ruby` gem to the Splunk management
885
+ port. That gem was last released in 2015 and does not verify the server's TLS certificate, so the
886
+ connection, including the login credentials, is open to interception.
624
887
 
625
888
  ### Grafana Loki
626
889
 
@@ -629,16 +892,141 @@ Send log entries to [Grafana Loki](https://grafana.com/docs/loki) via its
629
892
 
630
893
  ~~~ruby
631
894
  SemanticLogger.add_appender(
632
- appender: :loki,
633
- url: "https://logs-prod-001.grafana.net",
634
- username: "grafana_username",
635
- password: "grafana_token_here",
636
- compress: true
895
+ appender: :loki,
896
+ url: "https://logs-prod-001.grafana.net",
897
+ username: "grafana_username",
898
+ password: "grafana_token_here",
899
+ compress: true,
900
+ formatter: {loki: {structured_metadata: [:pid, :thread]}}
637
901
  )
638
902
  ~~~
639
903
 
640
904
  Set the URL, username, and password to match your Loki instance. Set `compress: true` to compress the
641
- log messages.
905
+ log messages. The ingestion path is appended to the url, so a query string on the url is kept and sent
906
+ with every push. `structured_metadata:` writes the process id and the thread name as structured
907
+ metadata rather than as labels, see
908
+ [pid and thread as structured metadata](#pid-and-thread-as-structured-metadata).
909
+
910
+ Each entry is a stream whose labels are `pid`, `host`, `application`, `environment`, `level`,
911
+ `thread`, `tags` (as JSON), each named tag, and each value an `on_log` subscriber adds to the
912
+ context, except `pid` and `thread` when they are structured metadata. Its line is the message. Its
913
+ structured metadata holds each payload key, and these fields when the entry has the value:
914
+
915
+ | Field | Value |
916
+ |-------|-------|
917
+ | `metric`, `metric_value`, `dimensions` | The metric's name, its amount, and its dimensions as JSON. |
918
+ | `duration`, `duration_human` | The duration in milliseconds, from the `measure_*` methods, and as text. |
919
+ | `exception_name`, `exception_message` | The class and message of the logged exception. |
920
+ | `stack_trace` | Its backtrace, followed by each exception that caused it. |
921
+ | `pid`, `thread` | The process id and the thread name, instead of as labels, when [`structured_metadata:`](#pid-and-thread-as-structured-metadata) names them. |
922
+
923
+ Structured metadata holds only Strings, so a nested value in the payload is written as JSON, and any
924
+ other value as its text. A payload key named like one of these fields is written with a `payload_`
925
+ prefix, as in `payload_duration`, whether or not the entry has that field, so that logged data cannot
926
+ replace them. Loki stores a name with characters other than ASCII letters and digits with each run of
927
+ them replaced by `_`, so a key that it would store under one of these names, such as `metric.value`,
928
+ is prefixed too, and written as Loki stores it: `payload_metric_value`. When another payload key has
929
+ that name once Loki has stored it, the prefix is added again, as in `payload_payload_metric_value`,
930
+ so that Loki keeps the value of each prefixed key. A key that is exactly one of these names gets the
931
+ name with one prefix, so `payload_metric_value` always holds the payload's `metric_value`. Loki keeps
932
+ only one value of other payload keys that it stores alike, such as `a.b` and `a_b`. Loki rejects a
933
+ payload key without an ASCII letter or digit, such as an empty key or `-`, so it is prefixed too, and
934
+ written as `payload_`.
935
+
936
+ Loki also rejects a label name other than an ASCII letter or underscore followed by letters, digits,
937
+ and underscores. A named tag or context key that it would reject, such as `request-id` or `1st`, is
938
+ written under the name Loki would give it as structured metadata, such as `request_id` or `key_1st`,
939
+ and an empty key as `key_`. A named tag or context value that is nil is left out.
940
+
941
+ Loki keeps one value of a label name, so a label whose name another label already has, such as a
942
+ named tag `host`, is written with the first number added that no other label has, as in `host_2`,
943
+ so that both values are kept. When the label of that name, or of a numbered name before it, has the
944
+ same value, as Loki stores them, it is not written again. The labels come in this order: the
945
+ formatter's own (`pid`, `application`, `environment`, `host`, `level`, `thread`, and `tags`, without
946
+ `pid` or `thread` when it is structured metadata), the named tags, and the context, each under its
947
+ own name; then each named tag and context key whose name was replaced, under that name when no label
948
+ has it yet; and then the rest, numbered, so that every label keeps the name it was written under. So
949
+ the named tags `host: "db-1", host_2: "db-2"` are written as `host_2="db-2"` and `host_3="db-1"`, and
950
+ so are `host: "db-1", "host-2": "db-2"`, in either order.
951
+
952
+ #### pid and thread as structured metadata
953
+
954
+ Loki [advises against](https://grafana.com/docs/loki/latest/get-started/labels/bp-labels/) labels
955
+ whose values are many and keep changing, as `pid` and `thread` do: every process restart, and every
956
+ thread, adds a stream, which Loki indexes and stores on its own. Its documentation names process and
957
+ thread ids as data for
958
+ [structured metadata](https://grafana.com/docs/loki/latest/get-started/labels/structured-metadata/)
959
+ instead. The formatter's `structured_metadata:` option names the labels to write there instead,
960
+ `:pid`, `:thread`, or both, where a query can still filter on them, as in
961
+ `{application="shop"} | thread="worker-1"`, but a stream selector cannot:
962
+
963
+ ~~~ruby
964
+ SemanticLogger.add_appender(
965
+ appender: :loki,
966
+ url: "https://logs-prod-001.grafana.net",
967
+ formatter: {loki: {structured_metadata: [:pid, :thread]}}
968
+ )
969
+ ~~~
970
+
971
+ Structured metadata needs a Loki that accepts it: one with schema v13, and with
972
+ `allow_structured_metadata`, which is on by default since Loki 3.0. Any other Loki rejects each
973
+ entry that has structured metadata, which with `:pid` is every entry, rather than only those with a
974
+ payload, a metric, a duration, or an exception.
975
+
976
+ A payload key named `pid` or `thread` is then written with the `payload_` prefix, as for the other
977
+ fields above. A named tag or context key of that name is written as a label with a number added, as
978
+ in `thread_2`, since Loki would otherwise show the structured metadata as `thread_extracted`.
979
+
980
+ By default both are labels, as with `structured_metadata: []`. Semantic Logger v6 writes both as
981
+ structured metadata by default, so building the formatter without `structured_metadata:`, as
982
+ `appender: :loki` does unless it is given one with `formatter: {loki: {...}}`, reports a
983
+ [deprecation warning](upgrading.html#seeing-deprecation-warnings). Pass `structured_metadata: []` to
984
+ keep them as labels without it.
985
+
986
+ #### Loki's label limits
987
+
988
+ By default Loki rejects the whole push, with every entry in it, for a stream with more than 15
989
+ labels, a label name longer than 1024 bytes, or a label value longer than 2048 bytes. So a label past
990
+ these limits is written to the structured metadata instead, where a query can still filter on it, as
991
+ in `{application="shop"} | tenant="acme"`, but a stream selector cannot:
992
+
993
+ * each label after the 15th, in the order above;
994
+ * a label with a longer name;
995
+ * a label with a longer value, such as many `tags`.
996
+
997
+ A label with an empty value, which Loki leaves out, is never moved, whatever the length of its name.
998
+ As Loki does, it is not counted, nor is `service_name`, which Loki adds to each stream.
999
+ A moved label is written as Loki stores structured metadata: as a String, under its name with each
1000
+ run of underscores as one, and with the `payload_` prefix when a payload key with its name would
1001
+ have it, as in `payload_duration`. When the structured metadata already has that name, a number is
1002
+ added, as in `tenant_2`, unless the only payload key that Loki stores under that name, such as
1003
+ `a.b` for `a_b`, has the same value, when the label is written once. So a moved label is numbered,
1004
+ as a label is, while a payload key whose prefixed name is taken gets the prefix again, as in
1005
+ `payload_payload_duration`.
1006
+
1007
+ When your Loki raises these limits in its `limits_config`, set the formatter's options of the same
1008
+ names to match, each an Integer of 1 or more:
1009
+
1010
+ ~~~ruby
1011
+ SemanticLogger.add_appender(
1012
+ appender: :loki,
1013
+ url: "https://logs-prod-001.grafana.net",
1014
+ formatter: {
1015
+ loki: {
1016
+ structured_metadata: [:pid, :thread],
1017
+ max_label_names_per_series: 30,
1018
+ max_label_name_length: 2048,
1019
+ max_label_value_length: 4096
1020
+ }
1021
+ }
1022
+ )
1023
+ ~~~
1024
+
1025
+ | Option | Default | Loki's limit |
1026
+ |--------|---------|--------------|
1027
+ | `max_label_names_per_series` | `15` | The most labels a stream can have. |
1028
+ | `max_label_name_length` | `1024` | The longest label name, in bytes. |
1029
+ | `max_label_value_length` | `2048` | The longest label value, in bytes. |
642
1030
 
643
1031
  ### CloudWatch Logs
644
1032
 
@@ -681,15 +1069,72 @@ SemanticLogger.add_appender(appender: :open_telemetry)
681
1069
  ~~~
682
1070
 
683
1071
  Each entry is emitted with its level mapped to the matching OpenTelemetry severity number, the message
684
- as the record body, and the payload as record attributes. The appender registers a
1072
+ as the record body, and the named tags and payload as record attributes. The appender registers a
685
1073
  `SemanticLogger.on_log` subscriber that captures the current OpenTelemetry context as each entry is
686
1074
  logged, so log records are correlated with the active trace and span.
687
1075
 
1076
+ For example, inside a span:
1077
+
1078
+ ~~~ruby
1079
+ SemanticLogger.named_tagged(request_id: "abc") do
1080
+ logger.info("Created user", user_id: 42)
1081
+ end
1082
+ ~~~
1083
+
1084
+ is emitted as a record with the severity text `info` and number `9`, the trace and span ids of that
1085
+ span, and:
1086
+
1087
+ ~~~
1088
+ body: "Created user"
1089
+ attributes: {"request_id" => "abc", "user_id" => 42, "semantic_logger.name" => "UserService",
1090
+ "thread.name" => "puma srv tp 001", "process.pid" => 12345}
1091
+ ~~~
1092
+
1093
+ Each named tag and payload key is an attribute of its own; a payload key takes precedence over a
1094
+ named tag of the same name. An entry without a message, such as `logger.error(exception)`, has no
1095
+ body. Semantic Logger adds these attributes when the entry has the value:
1096
+
1097
+ | Attribute | Value |
1098
+ |-----------|-------|
1099
+ | `exception.type`, `exception.message` | The class and message of the logged exception. |
1100
+ | `exception.stacktrace` | Its backtrace, followed by each exception that caused it. |
1101
+ | `code.file.path`, `code.line.number` | Where the entry was logged, when it has a call site (see [`call_site_level`](config.html#recording-where-each-entry-was-logged)) or captured a backtrace (see `backtrace_level`). |
1102
+ | `code.function.name` | The method that logged the entry, such as `Order#submit`, when it has a call site. |
1103
+ | `thread.name` | The name of the thread that logged the entry, or its object id when it has none. |
1104
+ | `process.pid` | The id of the process that logged the entry. |
1105
+ | `semantic_logger.name` | The logger's name, usually the class name. |
1106
+ | `semantic_logger.tags` | The tags, as an array. |
1107
+ | `semantic_logger.duration_ms` | The duration in milliseconds, from the `measure_*` methods. |
1108
+ | `semantic_logger.metric`, `semantic_logger.metric_amount` | The metric name and amount. |
1109
+ | `semantic_logger.dimensions.<key>` | Each of the metric's dimensions, such as `semantic_logger.dimensions.region`. |
1110
+
1111
+ The first six rows use the names from the
1112
+ [OpenTelemetry semantic conventions](https://opentelemetry.io/docs/specs/semconv/). A named tag or
1113
+ payload key named like one of these attributes is prefixed with its source, as in
1114
+ `payload.exception.type`, so that logged data cannot replace or forge them.
1115
+
1116
+ OpenTelemetry attribute values are Strings, Integers, Floats, booleans, or arrays of one of those
1117
+ types, so a nested Hash, Array, `Struct`, or `Set` is sent as JSON, any other value, such as a Symbol
1118
+ or a Time, as its `#to_s`, every element of an array that mixes types, such as `[1, "two"]`, as a
1119
+ String, and a `nil` value is left out.
1120
+
1121
+ The host name, application, and environment are not attributes: OpenTelemetry reports them once per
1122
+ export, in its resource, rather than on every record. Set them with the standard environment
1123
+ variables:
1124
+
1125
+ ~~~
1126
+ OTEL_SERVICE_NAME=my_app
1127
+ OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=production,host.name=web-1
1128
+ ~~~
1129
+
1130
+ The process id is an attribute because the resource holds the id of the process that configured
1131
+ OpenTelemetry, which is wrong for a worker forked from it, for example by Puma or Unicorn.
1132
+
688
1133
  | Option | Description |
689
1134
  |--------|-------------|
690
1135
  | `name` | Instrumentation scope name reported to OpenTelemetry. Defaults to `"SemanticLogger"`. |
691
1136
  | `version` | Instrumentation scope version. Defaults to the Semantic Logger gem version. |
692
- | `metrics` | Whether to forward metric-only log entries. Defaults to `true`. |
1137
+ | `metrics` | Whether to also send metric-only entries, as log records with no body. Defaults to `false`. |
693
1138
 
694
1139
  ### Logstash
695
1140
 
@@ -768,8 +1213,18 @@ for more.
768
1213
 
769
1214
  ## Error and exception monitoring
770
1215
 
771
- > **Note:** These appenders forward the payload as supplied. Take care not to push sensitive
772
- > information in tags or a payload.
1216
+ > **Note:** These appenders forward the payload and named tags. Name the keys whose values must not
1217
+ > be sent with [`SemanticLogger.sensitive_keys`](security.html#redacting-sensitive-data), and keep
1218
+ > sensitive information out of messages and tags, which are sent as they are.
1219
+
1220
+ The `:bugsnag`, `:sentry_ruby`, `:honeybadger`, and `:honeybadger_insights` appenders send the text
1221
+ of an entry, such as its message, payload, and named tags, converted to valid UTF-8, as the JSON
1222
+ formatters write it: binary text, such as a response body, is read as the UTF-8 it usually holds,
1223
+ and invalid bytes are dropped. A Hash or Array nested more than 32 levels deep, or that holds itself,
1224
+ is cut off there with a placeholder such as `"#<Hash nested too deeply>"`, as the JSON formatters cut
1225
+ it off. Any other object in the data is handed over unchanged, for the service to write. When an
1226
+ entry has an exception, `:bugsnag`, `:sentry_ruby`, and `:honeybadger` report that exception itself,
1227
+ whose message the service's gem cleans.
773
1228
 
774
1229
  ### Bugsnag
775
1230
 
@@ -800,23 +1255,40 @@ SemanticLogger.add_appender(appender: :sentry_ruby)
800
1255
  > **Deprecated:** the older `appender: :sentry` (backed by the end-of-life `sentry-raven` gem) still
801
1256
  > works but is deprecated and will be removed in v6. Switch to `appender: :sentry_ruby`.
802
1257
 
803
- Some logging context is forwarded to Sentry:
1258
+ Each entry is sent with the Sentry context of the thread (or fiber) that logged it: the user, tags,
1259
+ contexts, breadcrumbs, trace, and event processors that `sentry-ruby`, `sentry-rails`, or the
1260
+ application set for it, such as with `Sentry.set_user`. The request itself (its url, headers, and
1261
+ body) is left out, since Sentry reads the request body to send it, which the appender thread must not
1262
+ do while the request is using it; the errors that `sentry-rails` reports itself still include it. The appender sends the entry from its own thread, so it
1263
+ registers an `on_log` subscriber that keeps a copy of that Sentry scope with each entry it will send
1264
+ (with `Log#capture`, so that no formatter writes it), as the entry is logged.
1265
+
1266
+ Some logging context is also forwarded to Sentry, added to that of the thread:
804
1267
 
805
1268
  * From named tags, `transaction_name`. See
806
1269
  <https://docs.sentry.io/platforms/ruby/enriching-events/transaction-name/>.
807
- * From named tags or payload, `user` is built from the `user_id`, `username`, `user_email`, and
808
- `ip_address` keys, plus any `user` key that is itself a hash. See
1270
+ * From named tags, `user` is built from the `user_id`, `username`, `user_email`, and
1271
+ `ip_address` keys, plus any `user` key that is itself a hash. Its fields replace the same fields
1272
+ of the thread's Sentry user. See
809
1273
  <https://docs.sentry.io/platforms/ruby/enriching-events/identify-user/>.
810
- * From the payload, `fingerprint` configures grouping granularity. See
1274
+ * From named tags, `fingerprint` (an Array of Strings, Symbols, or numbers) configures grouping
1275
+ granularity. See
811
1276
  <https://docs.sentry.io/platforms/ruby/usage/sdk-fingerprinting/>.
812
1277
  * Named tags are sent as Sentry tags. See
813
1278
  <https://docs.sentry.io/platforms/ruby/enriching-events/tags/>.
814
1279
  * Unnamed tags are sent as the `:tag` tag, separated by commas.
815
- * Everything else from the payload and context is added to `extras`.
1280
+ * Everything else from the payload and context is added to `extras`, including a value of the
1281
+ wrong type for one of the keys above.
1282
+
1283
+ > **Deprecated:** the `user` and `fingerprint` keys are also still taken from the payload, but that
1284
+ > is deprecated and will be removed in v6, since the payload often holds untrusted data, such as a
1285
+ > request body, that could set the user an error is reported for. Set them with named tags. The
1286
+ > named tags win over the payload, and a user field is only taken from the payload when it is a
1287
+ > String, Symbol, or Integer.
816
1288
 
817
1289
  ~~~ruby
818
- SemanticLogger.tagged(transaction_name: "foo", user_id: 42, baz: "quz") do
819
- logger.error("some message", username: "joe", fingerprint: ["bar"])
1290
+ SemanticLogger.tagged(transaction_name: "foo", user_id: 42, username: "joe", fingerprint: ["bar"], baz: "quz") do
1291
+ logger.error("some message")
820
1292
  end
821
1293
  ~~~
822
1294
 
@@ -828,7 +1300,7 @@ Forward errors to Honeybadger (requires the `honeybadger` gem):
828
1300
  SemanticLogger.add_appender(appender: :honeybadger)
829
1301
  ~~~
830
1302
 
831
- Or forward all log entries to Honeybadger Insights as events:
1303
+ Or forward log entries at `:info` and above, its default level, to Honeybadger Insights as events:
832
1304
 
833
1305
  ~~~ruby
834
1306
  SemanticLogger.add_appender(appender: :honeybadger_insights)
@@ -868,6 +1340,10 @@ SemanticLogger.on_log do |log|
868
1340
  end
869
1341
  ~~~
870
1342
 
1343
+ It sends the whole entry, payload included, as it was logged: `on_log` subscribers run before
1344
+ [`SemanticLogger.sensitive_keys`](security.html#redacting-sensitive-data) redacts the entry, so its
1345
+ values are not filtered. Send only the fields Rollbar needs when entries can hold sensitive values.
1346
+
871
1347
  ## Databases and message queues
872
1348
 
873
1349
  ### MongoDB
@@ -901,6 +1377,12 @@ Each entry is stored as a document:
901
1377
  }
902
1378
  ~~~
903
1379
 
1380
+ MongoDB stores only valid UTF-8 text, and keys without NUL bytes. An entry holding other text, such
1381
+ as a binary response body in its payload, or an exception message that quotes invalid input, is
1382
+ stored with that text converted, as the JSON formatters write it: binary text is read as the UTF-8 it
1383
+ usually holds, invalid bytes are dropped, and NUL bytes are removed from keys. Such an entry nested
1384
+ more than 32 levels deep is cut off there, as the JSON formatters cut it off.
1385
+
904
1386
  ### Apache Kafka
905
1387
 
906
1388
  Publish log entries to an Apache Kafka broker (requires the `ruby-kafka` gem):
@@ -908,10 +1390,15 @@ Publish log entries to an Apache Kafka broker (requires the `ruby-kafka` gem):
908
1390
  ~~~ruby
909
1391
  SemanticLogger.add_appender(
910
1392
  appender: :kafka,
911
- seed_brokers: ["kafka1:9092", "kafka2:9092"]
1393
+ seed_brokers: ["kafka1:9092", "kafka2:9092"],
1394
+ metrics: false
912
1395
  )
913
1396
  ~~~
914
1397
 
1398
+ Pass `metrics: true` to also publish metric-only entries, which have a metric but no message and no
1399
+ exception. Without `metrics:`, it publishes them and reports a
1400
+ [deprecation warning](upgrading.html#seeing-deprecation-warnings), since v6 leaves them out by default.
1401
+
915
1402
  ### RabbitMQ (AMQP)
916
1403
 
917
1404
  Stream log entries through a queue on a RabbitMQ broker (requires the `bunny` gem):
@@ -922,10 +1409,34 @@ SemanticLogger.add_appender(
922
1409
  queue_name: "semantic_logger",
923
1410
  rabbitmq_host: "localhost",
924
1411
  username: "the-username",
925
- password: "the-password"
1412
+ password: "the-password",
1413
+ metrics: false
926
1414
  )
927
1415
  ~~~
928
1416
 
1417
+ Pass `metrics: true` to also send metric-only entries, which have a metric but no message and no
1418
+ exception. Without `metrics:`, it sends them and reports a
1419
+ [deprecation warning](upgrading.html#seeing-deprecation-warnings), since v6 leaves them out by default.
1420
+
1421
+ When it is reopened, such as with `SemanticLogger.reopen(force: true)`, it connects to RabbitMQ again,
1422
+ publishes the next entries on a new channel, and closes the connection it replaced. When it cannot
1423
+ connect, it keeps the connection it has. In a forked child, it leaves the connection it inherited to
1424
+ the parent (see [Process forking](operations.html#process-forking)).
1425
+
1426
+ RabbitMQ closes the channel when it refuses an entry, such as one larger than its `max_message_size`
1427
+ (`PRECONDITION_FAILED`). The appender then publishes the next entries on a new channel on the same
1428
+ connection, and reports the close to the internal logger. The refused entry is lost: RabbitMQ closes
1429
+ the channel after the entry was published, without answering the publish. While Bunny recovers the
1430
+ connection from a network failure, its channels are closed too: the appender then leaves them to
1431
+ Bunny, and the entries are written again once it has recovered. Once Bunny will not recover the
1432
+ connection, such as after its `recovery_attempts` have run out, or after a network failure with
1433
+ `automatic_recovery: false`, the appender closes it and publishes on a new connection. With
1434
+ `recover_from_connection_close: false` and automatic recovery on, Bunny does not recover a connection
1435
+ that RabbitMQ closed, and the appender cannot tell it from one about to be recovered: it raises until
1436
+ it is reopened, such as with `SemanticLogger.reopen(force: true)`. When Bunny itself fails to write
1437
+ an entry to the socket, such as with a `write_timeout:` set, it does not raise, so that entry is
1438
+ lost without a report.
1439
+
929
1440
  ## Logging to several destinations at once
930
1441
 
931
1442
  Add as many appenders as you like; every entry is written to all of them. Use a per-appender `level`