Class: RailsSemanticLogging::Puma::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_semantic_logging/puma.rb

Overview

Puma calls custom_logger.write(str) when the object responds to :write, otherwise it writes the line to stdout itself.

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Adapter

Returns a new instance of Adapter.



35
36
37
# File 'lib/rails_semantic_logging/puma.rb', line 35

def initialize(logger)
  @logger = logger
end

Instance Method Details

#write(str) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_semantic_logging/puma.rb', line 39

def write(str)
  message = str.to_s.chomp
  return if message.empty?

  # Under `puma -C config/puma.rb` the banner is emitted before Rails is
  # loaded, so the Railtie has not registered an appender yet and
  # SemanticLogger would drop the message with no output at all — worse
  # than the plain-text line we set out to replace. Fall back to stdout
  # until an appender exists. With `rails server` the app is loaded first
  # and this branch is never taken.
  if ::SemanticLogger.appenders.empty?
    $stdout.puts(message)
  else
    @logger.info(message)
  end
rescue StandardError
  # Puma logs "- Gracefully stopping, waiting for requests to finish"
  # from inside its SIGTERM trap handler (Single#stop_blocked, reached
  # from Launcher#setup_signals). Ruby forbids Mutex#synchronize in a
  # trap context, and SemanticLogger reaches one — directly or through
  # the datadog gem's SemanticLogger instrumentation — which raises
  # ThreadError and aborts the shutdown. Since SIGTERM is how containers
  # are stopped, a log line must never be able to take the server down:
  # emit it plainly and carry on.
  $stdout.puts(message)
end