Class: Shift::Api::Core::Middleware::Logger

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/shift/api/core/middleware/logger.rb

Overview

Faraday middleware to log requests and responses with request ids to a given logger. The logger must support the “info” method such as active support logger

Instance Method Summary collapse

Constructor Details

#initialize(app, logger:, id_generator: ::Shift::Api::Core::RequestId, formatter: ::Shift::Api::Core::Formatters::Logger) ⇒ Logger

Returns a new instance of Logger.



12
13
14
15
16
17
# File 'lib/shift/api/core/middleware/logger.rb', line 12

def initialize(app, logger:, id_generator: ::Shift::Api::Core::RequestId, formatter: ::Shift::Api::Core::Formatters::Logger)
  self.app = app
  self.logger = logger
  self.id_generator = id_generator
  self.formatter = formatter
end

Instance Method Details

#call(env) ⇒ Object

Logs the request and response to the logger

Parameters:

  • env (Faraday::Env)

    The environment from faraday



21
22
23
24
25
26
27
# File 'lib/shift/api/core/middleware/logger.rb', line 21

def call(env)
  request_id = id_generator.call
  logger.info formatter.message_from_request_body(env, request_id)
  app.call(env).on_complete do |env|
    logger.info formatter.message_from_response_body(env, request_id)
  end
end