Class: TelphinApi::Logger

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/telphin_api/logger.rb

Overview

Faraday middleware for logging requests and responses.

It's behaviour depends on the logging options in the configuration.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Logger

Creates a middleware instance. The logger is set from :logger configuration option.



8
9
10
11
# File 'lib/telphin_api/logger.rb', line 8

def initialize(app)
  super(app)
  @logger = TelphinApi.logger
end

Instance Method Details

#call(env) ⇒ Object

Logs the request if needed.

Parameters:

  • env (Hash)

    Request data.



15
16
17
18
19
20
21
22
# File 'lib/telphin_api/logger.rb', line 15

def call(env)
  if TelphinApi.log_requests?
    @logger.debug "#{env[:method].to_s.upcase} #{env[:url].to_s}"
    @logger.debug "body: #{env[:body].inspect}" unless env[:method] == :get
  end
  
  super
end

#on_complete(env) ⇒ Object

Logs the response (successful or not) if needed.

Parameters:

  • env (Hash)

    Response data.



26
27
28
29
30
31
32
# File 'lib/telphin_api/logger.rb', line 26

def on_complete(env)
  if env[:body].error?
    @logger.warn env[:raw_body] if TelphinApi.log_errors?
  else
    @logger.debug env[:raw_body] if TelphinApi.log_responses?
  end
end