Sha256: 2dbe65dbda4db93af9fcddd83d1a4ee45fe7a07a6cdd5095c12950b092a1b84f

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module ZaloAPI
  module Middleware
    module Response
      # Faraday middleware to handle logging
      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

2 entries across 2 versions & 1 rubygems

Version Path
zalo-api-0.1.1 lib/zalo_api/middleware/response/logger.rb
zalo-api-0.1.0 lib/zalo_api/middleware/response/logger.rb