Class: Shift::Api::Core::Middleware::Logger
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Shift::Api::Core::Middleware::Logger
- 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
-
#call(env) ⇒ Object
Logs the request and response to the logger.
-
#initialize(app, logger:, id_generator: ::Shift::Api::Core::RequestId, formatter: ::Shift::Api::Core::Formatters::Logger) ⇒ Logger
constructor
A new instance of Logger.
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
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.(env, request_id) app.call(env).on_complete do |env| logger.info formatter.(env, request_id) end end |