Sha256: c5f98e4de59f7b328413e8c733a089a9ad0f772283f718ac43977ce6611aa5d3

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

require 'forwardable'

module Faraday
  class Response::Logger < Response::Middleware
    extend Forwardable

    DEFAULT_OPTIONS = { :bodies => false }

    def initialize(app, logger = nil, options = {})
      super(app)
      @logger = logger || begin
        require 'logger'
        ::Logger.new(STDOUT)
      end
      @options = DEFAULT_OPTIONS.merge(options)
    end

    def_delegators :@logger, :debug, :info, :warn, :error, :fatal

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

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

    private

    def dump_headers(headers)
      headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
    end

    def dump_body(body)
      if body.respond_to?(:to_str)
        body.to_str
      else
        pretty_inspect(body)
      end
    end

    def pretty_inspect(body)
      require 'pp' unless body.respond_to?(:pretty_inspect)
      body.pretty_inspect
    end

    def log_body?(type)
      case @options[:bodies]
      when Hash then @options[:bodies][type]
      else @options[:bodies]
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 5 rubygems

Version Path
mercadopago-custom-checkout-0.1.2 .gs/gems/faraday-0.9.2/lib/faraday/response/logger.rb
mercadopago-custom-checkout-0.1.1 .gs/gems/faraday-0.9.2/lib/faraday/response/logger.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/faraday-0.9.2/lib/faraday/response/logger.rb
second_step-0.1.2 secondstep-notify-1.0.0-osx/lib/ruby/lib/ruby/gems/2.2.0/gems/faraday-0.9.2/lib/faraday/response/logger.rb
tdiary-5.0.2 vendor/bundle/gems/faraday-0.9.2/lib/faraday/response/logger.rb
tdiary-5.0.1 vendor/bundle/gems/faraday-0.9.2/lib/faraday/response/logger.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/faraday-0.9.2/lib/faraday/response/logger.rb
faraday-0.9.2 lib/faraday/response/logger.rb
faraday-0.9.1 lib/faraday/response/logger.rb