Sha256: 73acaea68fc24610a7db7cae4d219f25b5ac9e75c4cbcfb0f9b8b4577ee18d09

Contents?: true

Size: 1.12 KB

Versions: 37

Compression:

Stored size: 1.12 KB

Contents

module ZendeskAPI
  module Middleware
    module Response
      # Faraday middleware to handle logging
      # @private
      class Logger < Faraday::Middleware
        LOG_LENGTH = 1000

        def initialize(app, logger = nil)
          super(app)

          @logger = logger || begin
            require 'logger'
            ::Logger.new(STDOUT)
          end
        end

        def call(env)
          @logger.info "#{env[:method]} #{env[:url].to_s}"
          @logger.debug dump_debug(env, :request_headers)

          @app.call(env).on_complete do |env|
            info = "Status #{env[:status]}"
            info.concat(" #{env[:body].to_s[0, LOG_LENGTH]}") if (400..499).cover?(env[:status].to_i)

            @logger.info info
            @logger.debug dump_debug(env, :response_headers)
          end
        end

        private

        def dump_debug(env, headers_key)
          info = env[headers_key].map { |k, v| "  #{k}: #{v.inspect}" }.join("\n")
          unless env[:body].nil?
            info.concat("\n")
            info.concat(env[:body].inspect)
          end
          info
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 2 rubygems

Version Path
zendesk_api-3.1.1 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-3.1.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-3.0.5 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-3.0.4 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-3.0.2 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-3.0.1 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-3.0.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-3.0.0.rc1 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-2.0.1 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-2.0.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.38.0.rc2 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.38.0.rc1 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.37.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.36.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.35.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.34.0 lib/zendesk_api/middleware/response/logger.rb
discourse_zendesk_api-1.0.1 lib/zendesk_api/middleware/response/logger.rb
discourse_zendesk_api-1.0.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.33.0 lib/zendesk_api/middleware/response/logger.rb
zendesk_api-1.32.0 lib/zendesk_api/middleware/response/logger.rb