Class: Cogger::Rack::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/cogger/rack/logger.rb

Overview

Middlware for enriched logging based on the incoming request.

Constant Summary collapse

DEFAULTS =
{
  logger: Cogger.new(formatter: :json),
  timer: Cogger::Time::Span.new,
  key_map: {
    verb: "REQUEST_METHOD",
    ip: "REMOTE_ADDR",
    path: "PATH_INFO",
    params: "QUERY_STRING",
    length: "CONTENT_LENGTH"
  }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(application, options = Core::EMPTY_HASH, defaults: DEFAULTS) ⇒ Logger

Returns a new instance of Logger.



21
22
23
24
25
26
27
28
# File 'lib/cogger/rack/logger.rb', line 21

def initialize application, options = Core::EMPTY_HASH, defaults: DEFAULTS
  configuration = defaults.merge options

  @application = application
  @logger = configuration.fetch :logger
  @timer = configuration.fetch :timer
  @key_map = configuration.fetch :key_map
end

Instance Method Details

#call(environment) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/cogger/rack/logger.rb', line 30

def call environment
  request = ::Rack::Request.new environment
  (status, headers, body), duration, unit = timer.call { application.call environment }

  logger.info tags: [tags_for(request), {status:, duration:, unit:}]

  [status, headers, body]
end