Module: RailsSemanticLogging::Puma

Defined in:
lib/rails_semantic_logging/puma.rb

Overview

Routes Puma's own output through SemanticLogger so the boot banner and lifecycle lines become structured events instead of raw text.

Without this, Puma writes those lines straight to stdout. In a container that means the log collector ingests one plain-text event per line, tagged with whatever host it infers rather than the pod, and none of them carry the attributes every other line in the app has.

This cannot be wired from the Railtie: custom_logger lives on Puma's configuration DSL, which only exists while config/puma.rb is evaluated. By the time Rails boots, Puma::Launcher has already read the option (launcher.rb applies @options[:custom_logger] in its constructor), and the Puma::LogWriter in use is a fresh instance built by the Rack handler rather than the reachable Puma::LogWriter::DEFAULT singleton. So the app has to opt in explicitly:

# config/puma.rb
require 'rails_semantic_logging/puma'
RailsSemanticLogging::Puma.activate(self)

Note this covers Puma's own output only. The three => Booting Puma / => Rails ... starting / => Run bin/rails server --help lines come from Rails::Command::ServerCommand#print_boot_information, which writes to $stdout through Thor's say with no logger in between, and cannot be captured here.

Defined Under Namespace

Classes: Adapter

Class Method Summary collapse

Class Method Details

.activate(puma_config, logger_name: 'Puma') ⇒ Adapter?

Returns the installed adapter, or nil when unsupported.

Parameters:

  • puma_config (Puma::DSL)

    the self of config/puma.rb

  • logger_name (String) (defaults to: 'Puma')

    name the events are logged under

Returns:

  • (Adapter, nil)

    the installed adapter, or nil when unsupported



72
73
74
75
76
77
78
79
80
# File 'lib/rails_semantic_logging/puma.rb', line 72

def activate(puma_config, logger_name: 'Puma')
  # `custom_logger` was added in Puma 6.2.0. Degrade quietly on older
  # versions rather than raising from a boot file.
  return unless puma_config.respond_to?(:custom_logger)

  Adapter.new(::SemanticLogger[logger_name]).tap do |adapter|
    puma_config.custom_logger(adapter)
  end
end