Sha256: c5300f3b4d31bbe6a8d67c628bc8251e5dc790b8b4bb563d57dae49c29fb2150

Contents?: true

Size: 1.42 KB

Versions: 35

Compression:

Stored size: 1.42 KB

Contents

require 'faraday'

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

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

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

        def call(env)
          debug "=> URL: #{env.method} #{env.url}, Headers: #{dump_headers env.request_headers}"
          debug "=> Body: #{body_for(env)}"
          super
        end

        def on_complete(env)
          debug "<= Status: #{env.status}, Headers: #{dump_headers env.response_headers}"
          debug "<= Body: #{body_for(env)}"
        end

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

        def body_for(env)
          return '' if !env.body || env.body.empty?

          if env.request_headers['Content-Type'] == 'application/x-msgpack'
            MessagePack.unpack(env.body)
          else
            env.body
          end

        rescue StandardError
          readable_body(env.body)
        end

        def readable_body(body)
          if body.respond_to?(:encoding) && body.encoding == Encoding::ASCII_8BIT
            body.unpack('H*')
          else
            body
          end
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
ably-0.8.5 lib/ably/rest/middleware/logger.rb
ably-0.8.4 lib/ably/rest/middleware/logger.rb
ably-rest-0.8.3 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.8.3 lib/ably/rest/middleware/logger.rb
ably-rest-0.8.2 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.8.2 lib/ably/rest/middleware/logger.rb
ably-0.8.1 lib/ably/rest/middleware/logger.rb
ably-rest-0.8.1 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.8.0 lib/ably/rest/middleware/logger.rb
ably-0.7.6 lib/ably/rest/middleware/logger.rb
ably-rest-0.7.5 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.7.5 lib/ably/rest/middleware/logger.rb
ably-0.7.4 lib/ably/rest/middleware/logger.rb
ably-rest-0.7.3 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.7.2 lib/ably/rest/middleware/logger.rb