Class: Restforce::Middleware::Logger

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

Instance Method Summary collapse

Constructor Details

#initialize(app, logger, options) ⇒ Logger

Returns a new instance of Logger.



9
10
11
12
13
14
15
16
# File 'lib/restforce/middleware/logger.rb', line 9

def initialize(app, logger, options)
  super(app)
  @options = options
  @logger = logger || begin
    require 'logger'
    ::Logger.new($stdout)
  end
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  debug('request') do
    dump url: env[:url].to_s,
         method: env[:method],
         headers: env[:request_headers],
         body: env[:body]
  end

  on_request(env) if respond_to?(:on_request)
  @app.call(env).on_complete do |environment|
    on_complete(environment) if respond_to?(:on_complete)
  end
end

#dump(hash) ⇒ Object



42
43
44
45
# File 'lib/restforce/middleware/logger.rb', line 42

def dump(hash)
  dumped_pairs = hash.map { |k, v| "  #{k}: #{v.inspect}" }.join("\n")
  "\n#{dumped_pairs}"
end

#on_complete(env) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/restforce/middleware/logger.rb', line 34

def on_complete(env)
  debug('response') do
    dump status: env[:status].to_s,
         headers: env[:response_headers],
         body: env[:body]
  end
end