Class: BookingSync::API::Middleware::Logger

Inherits:
Faraday::Middleware
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bookingsync/api/middleware/logger.rb

Overview

Provides logger for request and responses made by API Client. JSON data is displayed in an eye-friendly format. User can provide his own logger in BookingSync::API::Client.new

Log Levels

INFO - logged are only request method and URL.
DEBUG - logged are headers and bodies of requests and responses.

Instance Method Summary collapse

Constructor Details

#initialize(app, logger) ⇒ Logger

Returns a new instance of Logger.



14
15
16
17
# File 'lib/bookingsync/api/middleware/logger.rb', line 14

def initialize(app, logger)
  super(app)
  @logger = logger
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bookingsync/api/middleware/logger.rb', line 21

def call(env)
  info "Request #{env.method.upcase} #{env.url.to_s}"
  debug("Request headers") { dump_headers env.request_headers }
  debug("Request body") { dump_body env.body }
  @app.call(env).tap do |response|
    info "Response X-Request-Id: #{env.response_headers['X-Request-Id']} #{env.method.upcase} #{env.url.to_s}"
    debug("Response headers") { dump_headers response.env.response_headers }
    debug("Response status") { response.env.status }
    debug("Response body") { dump_body response.env.body }
  end
end