Sha256: 9c66503929dd8c21ca4937c48f72e203e9ede921948973af83f2ae2420c31baa

Contents?: true

Size: 1.63 KB

Versions: 8

Compression:

Stored size: 1.63 KB

Contents

module Beaker
  module Http
    class FaradayBeakerLogger < Faraday::Response::Middleware
      extend Forwardable

      DEFAULT_OPTIONS = { :bodies => true }

      def initialize(app, logger, options = {} )
        super(app)
        @logger = logger
        @options = DEFAULT_OPTIONS.merge(options)
      end

      def_delegators :@logger, :trace, :debug, :info, :notify, :warn

      def call(env)
        @start_time = Time.now
        info "#{env.method.upcase}: #{env.url.to_s}"
        debug "REQUEST HEADERS:\n#{dump_headers env.request_headers}"
        debug "REQUEST BODY:\n#{dump_body env[:body]}" if env[:body] && log_body?(:request)
        super
      end

      def on_complete(env)
        info "RESPONSE CODE: #{env.status.to_s}"
        debug "ELAPSED TIME: #{Time.now - @start_time}"
        debug "RESPONSE HEADERS:\n#{dump_headers env.response_headers}"
        debug "RESPONSE BODY:\n#{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 pretty_inspect(body)
        require 'pp' unless body.respond_to?(:pretty_inspect)
        body.pretty_inspect
      end

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

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

    end
  end
end

Faraday::Response.register_middleware :faraday_beaker_logger => lambda { Beaker::Http::FaradayBeakerLogger }

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
scooter-4.5.4 lib/beaker-http/middleware/response/faraday_beaker_logger.rb
scooter-4.5.3 lib/beaker-http/middleware/response/faraday_beaker_logger.rb
scooter-4.5.2 lib/beaker-http/middleware/response/faraday_beaker_logger.rb
scooter-4.5.1 lib/beaker-http/middleware/response/faraday_beaker_logger.rb
scooter-4.5.0 lib/beaker-http/middleware/response/faraday_beaker_logger.rb
scooter-4.4.0 lib/beaker-http/middleware/response/faraday_beaker_logger.rb
beaker-http-0.2.0 lib/beaker-http/middleware/response/faraday_beaker_logger.rb
beaker-http-0.1.0 lib/beaker-http/middleware/response/faraday_beaker_logger.rb