class Faraday::Response::Logger
Constants
- DEFAULT_OPTIONS
Public Class Methods
new(app, logger = nil, options = {}) { |self| ... }
click to toggle source
Calls superclass method
Faraday::Middleware::new
# 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
Public Instance Methods
call(env)
click to toggle source
Calls superclass method
Faraday::Response::Middleware#call
# 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)
click to toggle source
# File lib/faraday/response/logger.rb, line 35 def filter(filter_word, filter_replacement) @filter.push([ filter_word, filter_replacement ]) end
on_complete(env)
click to toggle source
# 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
Private Instance Methods
apply_filters(output)
click to toggle source
# File lib/faraday/response/logger.rb, line 72 def apply_filters(output) @filter.each do |pattern, replacement| output = output.to_s.gsub(pattern, replacement) end output end
dump_body(body)
click to toggle source
# File lib/faraday/response/logger.rb, line 45 def dump_body(body) if body.respond_to?(:to_str) body.to_str else pretty_inspect(body) end end
dump_headers(headers)
click to toggle source
# File lib/faraday/response/logger.rb, line 41 def dump_headers(headers) headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") end
log_body?(type)
click to toggle source
# File lib/faraday/response/logger.rb, line 65 def log_body?(type) case @options[:bodies] when Hash then @options[:bodies][type] else @options[:bodies] end end
log_headers?(type)
click to toggle source
# File lib/faraday/response/logger.rb, line 58 def log_headers?(type) case @options[:headers] when Hash then @options[:headers][type] else @options[:headers] end end
pretty_inspect(body)
click to toggle source
# File lib/faraday/response/logger.rb, line 53 def pretty_inspect(body) require 'pp' unless body.respond_to?(:pretty_inspect) body.pretty_inspect end