Class: Faraday::Response::Logger

Inherits:
Middleware show all
Extended by:
Forwardable
Defined in:
lib/faraday/response/logger.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ :headers => true, :bodies => false }

Instance Method Summary collapse

Methods inherited from Middleware

dependency, inherited, loaded?, new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware

Constructor Details

#initialize(app, logger = nil, options = {}) {|_self| ... } ⇒ Logger

Returns a new instance of Logger.

Yields:

  • (_self)

Yield Parameters:


9
10
11
12
13
14
15
16
17
18
# File 'lib/faraday/response/logger.rb', line 9

def initialize(app, logger = nil, options = {})
  super(app)
  @logger = logger || begin
    require 'logger'
    ::Logger.new($stdout)
  end
  @filter = []
  @options = DEFAULT_OPTIONS.merge(options)
  yield self if block_given?
end

Instance Method Details

#call(env) ⇒ Object


22
23
24
25
26
27
# File 'lib/faraday/response/logger.rb', line 22

def call(env)
  info('request')  { "#{env.method.upcase} #{apply_filters(env.url.to_s)}" }
  debug('request') { apply_filters( dump_headers env.request_headers ) } if log_headers?(:request)
  debug('request') { apply_filters( dump_body(env[:body]) ) } if env[:body] && log_body?(:request)
  super
end

#filter(filter_word, filter_replacement) ⇒ Object


35
36
37
# File 'lib/faraday/response/logger.rb', line 35

def filter(filter_word, filter_replacement)
  @filter.push([ filter_word, filter_replacement ])
end

#on_complete(env) ⇒ Object


29
30
31
32
33
# File 'lib/faraday/response/logger.rb', line 29

def on_complete(env)
  info('response')  { "Status #{env.status.to_s}" }
  debug('response') { apply_filters( dump_headers env.response_headers ) } if log_headers?(:response)
  debug('response') { apply_filters( dump_body env[:body] ) } if env[:body] && log_body?(:response)
end