Sha256: fbe01c5f390c59d46a5bcb1d1a5128b1c346ad198806786e87b460af6bfabc7e

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

require "faraday"

# @private
module FaradayMiddleware
  # @private
  class LoudLogger < Faraday::Middleware
    extend Forwardable
    def_delegators :@logger, :debug, :info, :warn, :error, :fatal

    def initialize(app, options = {})
      @app = app
      @logger = options.fetch(:logger) do
        require "logger"
        ::Logger.new($stdout)
      end
    end

    def call(env)
      start_time = Time.now
      info  { request_info(env) }
      debug { request_debug(env) }
      @app.call(env).on_complete do
        end_time = Time.now
        response_time = end_time - start_time
        info  { response_info(env, response_time) }
        debug { response_debug(env) }
      end
    end

    private

    def filter(output)
      if ENV["INSTAGRAM_GEM_REDACT"]
        output = output.to_s.gsub(/client_id=[a-zA-Z0-9]*/, "client_id=[CLIENT-ID]")
        output = output.to_s.gsub(/access_token=[a-zA-Z0-9]*/, "access_token=[ACCESS-TOKEN]")
      else
        output
      end
    end

    def request_info(env)
      "Started %s request to: %s" % [env[:method].to_s.upcase, filter(env[:url])]
    end

    def response_info(env, response_time)
      "Response from %s; Status: %d; Time: %.1fms" % [filter(env[:url]), env[:status], (response_time * 1_000.0)]
    end

    def request_debug(env)
      debug_message("Request", env[:request_headers], env[:body])
    end

    def response_debug(env)
      debug_message("Response", env[:response_headers], env[:body])
    end

    def debug_message(name, headers, body)
      <<-MESSAGE.gsub(/^ +([^ ])/m, '\\1')
        #{name} Headers:
        ----------------
        #{format_headers(headers)}

        #{name} Body:
        -------------
        #{filter(body)}
        MESSAGE
    end

    def format_headers(headers)
      length = headers.map { |k, _v| k.to_s.size }.max
      headers.map { |name, value| "#{name.to_s.ljust(length)} : #{filter(value)}" }.join("\n")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
instagram-continued-1.4.0 lib/faraday/loud_logger.rb
instagram-continued-1.3.3 lib/faraday/loud_logger.rb
instagram-continued-1.3.2 lib/faraday/loud_logger.rb
instagram-continued-1.3.1 lib/faraday/loud_logger.rb