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-rest-0.8.15 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.8.15 lib/ably/rest/middleware/logger.rb
ably-rest-0.9.3 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-rest-0.9.2 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-rest-0.9.1 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-rest-0.9.0 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-rest-0.8.14 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.8.14 lib/ably/rest/middleware/logger.rb
ably-rest-0.8.13 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.8.13 lib/ably/rest/middleware/logger.rb
ably-0.8.12 lib/ably/rest/middleware/logger.rb
ably-0.8.11 lib/ably/rest/middleware/logger.rb
ably-0.8.10 lib/ably/rest/middleware/logger.rb
ably-rest-0.8.9 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-0.8.9 lib/ably/rest/middleware/logger.rb
ably-0.8.8 lib/ably/rest/middleware/logger.rb
ably-0.8.7 lib/ably/rest/middleware/logger.rb
ably-0.8.6 lib/ably/rest/middleware/logger.rb
ably-rest-0.8.6 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb
ably-rest-0.8.5 lib/submodules/ably-ruby/lib/ably/rest/middleware/logger.rb