rails_semantic_logging 0.3.0 → 0.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8dabf186d9982fb21140d284857db16d25867425b0f9430bc51ba6513851bcc8
4
- data.tar.gz: '03886b8629bef05d49a21f11d5e59dd434740e6d40b1b2e9de24bffe4929a1a0'
3
+ metadata.gz: 505f93635024c16b36a3535ec71c948ac41e570b2f0aef012672596733d38a16
4
+ data.tar.gz: a0fe323394001c28b2a1e34962ce5aa631b8641e137bf4438d747a1e39d7947e
5
5
  SHA512:
6
- metadata.gz: 1bd15a9bbce788f3dbccac1f52d2ef0bbbddfe6838b1532abe0fce62c0982a7a29d84b71b3ac028eafa9a721d4485972e5b0d402d15189b21f07ea7110c79e62
7
- data.tar.gz: 96836a1fd5830939269323239819805c293285d76889c86bdbef6fd3a343b5250ade36266a939658ee1f0fdb82b7cddac1cc5371238115860dcca5ce8cd7cf27
6
+ metadata.gz: 726ceadd1533d5dd4947eba6e4408ffb7344dc6a860a511327dea109eb4aca87ea102309d25e00d3a2aa8aa1f7a96b205adf88e934a14b7497a42ff73ae33675
7
+ data.tar.gz: 35fe8fbeb74a493deacb0bed229e64405ba04f28d4057836a1f7d375f5f7de62aaed1e26a55ed8af54a89bb5c313ed62a29718e3a2722c7d09494e5d1c31b986
data/README.md CHANGED
@@ -279,6 +279,55 @@ The matcher passes a **duplicated** copy of the log event to the formatter, so a
279
279
 
280
280
  ## Puma Integration
281
281
 
282
+ ### Routing Puma's own output through SemanticLogger
283
+
284
+ Puma writes its boot banner and lifecycle lines straight to stdout, so they stay
285
+ plain text while everything else in the app is structured. In a container that
286
+ means one unparsed event per line, carrying none of the attributes the rest of
287
+ your logs have.
288
+
289
+ Opt in from `config/puma.rb`:
290
+
291
+ ```ruby
292
+ # config/puma.rb
293
+ require 'rails_semantic_logging/puma'
294
+ RailsSemanticLogging::Puma.activate(self)
295
+ ```
296
+
297
+ `Puma starting in single mode...`, `* Puma version`, `* Ruby version`,
298
+ `* Min/Max threads`, `* Environment`, `* PID`, `* Listening on`,
299
+ `* Starting control server` and `Use Ctrl-C to stop` then go through the
300
+ configured formatter under the logger name `Puma` (override with
301
+ `activate(self, logger_name: '...')`), so they can be selected with
302
+ `logger.name:Puma` alongside the rest of the application logs.
303
+
304
+ Requires **Puma >= 6.2.0**, which introduced the `custom_logger` DSL option. On
305
+ older versions `activate` returns `false` and changes nothing, so it is safe to
306
+ leave in a shared config.
307
+
308
+ It has to be called from `config/puma.rb` rather than wired automatically by the
309
+ Railtie: `custom_logger` lives on Puma's configuration DSL, which only exists
310
+ while that file is being evaluated. By the time Rails boots, `Puma::Launcher`
311
+ has already read the option.
312
+
313
+ Two things it deliberately does not cover:
314
+
315
+ - The `=> Booting Puma` / `=> Rails ... starting` / `=> Run bin/rails server --help`
316
+ lines. Those come from `Rails::Command::ServerCommand#print_boot_information`,
317
+ which writes to `$stdout` through Thor's `say` with no logger in between.
318
+ - Under `puma -C config/puma.rb` (as opposed to `rails server`) the banner is
319
+ emitted before Rails is loaded, so no appender exists yet. Rather than let
320
+ SemanticLogger drop those lines silently, the adapter falls back to writing
321
+ them to stdout until an appender is registered.
322
+ - `- Gracefully stopping, waiting for requests to finish` is logged from inside
323
+ Puma's SIGTERM trap handler, and Ruby forbids `Mutex#synchronize` in a trap
324
+ context — which SemanticLogger reaches, directly or through the `datadog`
325
+ gem's SemanticLogger instrumentation. The adapter catches that and writes the
326
+ line to stdout instead, so a log line can never abort a shutdown. Since
327
+ SIGTERM is how containers are stopped, this one matters.
328
+
329
+ ### Forking and appender reopen
330
+
282
331
  With `semantic_logger` >= 5 appenders are reopened automatically after forking, so no Puma
283
332
  configuration is needed (opt out with `SemanticLogger.reopen_on_fork = false`).
284
333
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.2
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'semantic_logger'
4
+
5
+ module RailsSemanticLogging
6
+ # Routes Puma's own output through SemanticLogger so the boot banner and
7
+ # lifecycle lines become structured events instead of raw text.
8
+ #
9
+ # Without this, Puma writes those lines straight to stdout. In a container
10
+ # that means the log collector ingests one plain-text event per line, tagged
11
+ # with whatever host it infers rather than the pod, and none of them carry
12
+ # the attributes every other line in the app has.
13
+ #
14
+ # This cannot be wired from the Railtie: `custom_logger` lives on Puma's
15
+ # configuration DSL, which only exists while `config/puma.rb` is evaluated.
16
+ # By the time Rails boots, `Puma::Launcher` has already read the option
17
+ # (launcher.rb applies `@options[:custom_logger]` in its constructor), and
18
+ # the `Puma::LogWriter` in use is a fresh instance built by the Rack handler
19
+ # rather than the reachable `Puma::LogWriter::DEFAULT` singleton. So the app
20
+ # has to opt in explicitly:
21
+ #
22
+ # # config/puma.rb
23
+ # require 'rails_semantic_logging/puma'
24
+ # RailsSemanticLogging::Puma.activate(self)
25
+ #
26
+ # Note this covers Puma's own output only. The three `=> Booting Puma` /
27
+ # `=> Rails ... starting` / `=> Run bin/rails server --help` lines come from
28
+ # `Rails::Command::ServerCommand#print_boot_information`, which writes to
29
+ # `$stdout` through Thor's `say` with no logger in between, and cannot be
30
+ # captured here.
31
+ module Puma
32
+ # Puma calls `custom_logger.write(str)` when the object responds to
33
+ # `:write`, otherwise it writes the line to stdout itself.
34
+ class Adapter
35
+ def initialize(logger)
36
+ @logger = logger
37
+ end
38
+
39
+ def write(str)
40
+ message = str.to_s.chomp
41
+ return if message.empty?
42
+
43
+ # Under `puma -C config/puma.rb` the banner is emitted before Rails is
44
+ # loaded, so the Railtie has not registered an appender yet and
45
+ # SemanticLogger would drop the message with no output at all — worse
46
+ # than the plain-text line we set out to replace. Fall back to stdout
47
+ # until an appender exists. With `rails server` the app is loaded first
48
+ # and this branch is never taken.
49
+ if ::SemanticLogger.appenders.empty?
50
+ $stdout.puts(message)
51
+ else
52
+ @logger.info(message)
53
+ end
54
+ rescue StandardError
55
+ # Puma logs "- Gracefully stopping, waiting for requests to finish"
56
+ # from inside its SIGTERM trap handler (Single#stop_blocked, reached
57
+ # from Launcher#setup_signals). Ruby forbids Mutex#synchronize in a
58
+ # trap context, and SemanticLogger reaches one — directly or through
59
+ # the datadog gem's SemanticLogger instrumentation — which raises
60
+ # ThreadError and aborts the shutdown. Since SIGTERM is how containers
61
+ # are stopped, a log line must never be able to take the server down:
62
+ # emit it plainly and carry on.
63
+ $stdout.puts(message)
64
+ end
65
+ end
66
+
67
+ module_function
68
+
69
+ # @param puma_config [Puma::DSL] the `self` of `config/puma.rb`
70
+ # @param logger_name [String] name the events are logged under
71
+ # @return [Adapter, nil] the installed adapter, or nil when unsupported
72
+ def activate(puma_config, logger_name: 'Puma')
73
+ # `custom_logger` was added in Puma 6.2.0. Degrade quietly on older
74
+ # versions rather than raising from a boot file.
75
+ return unless puma_config.respond_to?(:custom_logger)
76
+
77
+ Adapter.new(::SemanticLogger[logger_name]).tap do |adapter|
78
+ puma_config.custom_logger(adapter)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -18,18 +18,10 @@ module RailsSemanticLogging
18
18
  # Declare a single stdout appender with the configured formatter.
19
19
  # IMPORTANT: Do NOT pass level: parameter. Subscriber#level defaults to :trace
20
20
  # when unset, which is required by the host app's spec/support/output.rb check.
21
- if app.config.rails_semantic_logger.respond_to?(:appenders?)
22
- # rails_semantic_logger >= 5: declaring an appender replaces the default
23
- # file appender and the automatic console stderr appender.
24
- app.config.rails_semantic_logger.appenders do |appenders|
25
- appenders.add(io: $stdout, formatter: formatter)
26
- end
27
- else
28
- # rails_semantic_logger 4.x: disable the built-in appenders and add ours.
29
- app.config.rails_semantic_logger.console_logger = false
30
- app.config.rails_semantic_logger.add_file_appender = false
31
- app.config.rails_semantic_logger.format = formatter
32
- app.config.semantic_logger.add_appender(io: $stdout, formatter: formatter)
21
+ # Declaring an appender replaces the default file appender and the
22
+ # automatic console stderr appender.
23
+ app.config.rails_semantic_logger.appenders do |appenders|
24
+ appenders.add(io: $stdout, formatter: formatter)
33
25
  end
34
26
 
35
27
  # Merge default tags (request_id, client_ip) with app-specific custom tags
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_semantic_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Napoleoni
@@ -29,7 +29,7 @@ dependencies:
29
29
  requirements:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: '7.1'
32
+ version: '7.2'
33
33
  - - "<"
34
34
  - !ruby/object:Gem::Version
35
35
  version: '9'
@@ -39,7 +39,7 @@ dependencies:
39
39
  requirements:
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: '7.1'
42
+ version: '7.2'
43
43
  - - "<"
44
44
  - !ruby/object:Gem::Version
45
45
  version: '9'
@@ -49,7 +49,7 @@ dependencies:
49
49
  requirements:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: '4.0'
52
+ version: '5.0'
53
53
  - - "<"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '6'
@@ -59,7 +59,7 @@ dependencies:
59
59
  requirements:
60
60
  - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: '4.0'
62
+ version: '5.0'
63
63
  - - "<"
64
64
  - !ruby/object:Gem::Version
65
65
  version: '6'
@@ -82,6 +82,7 @@ files:
82
82
  - lib/rails_semantic_logging/formatters/datadog.rb
83
83
  - lib/rails_semantic_logging/job_logging/active_job_patch.rb
84
84
  - lib/rails_semantic_logging/job_logging/sidekiq_patch.rb
85
+ - lib/rails_semantic_logging/puma.rb
85
86
  - lib/rails_semantic_logging/railtie.rb
86
87
  - lib/rails_semantic_logging/rspec/helpers.rb
87
88
  - lib/rails_semantic_logging/rspec/matchers.rb
@@ -100,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
101
  requirements:
101
102
  - - ">="
102
103
  - !ruby/object:Gem::Version
103
- version: '3.2'
104
+ version: '3.3'
104
105
  required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  requirements:
106
107
  - - ">="