Sha256: 239c01c9ede5b1b7094c94dd39a234f5fb969a45f648ba9124f62c58c98e2ab1

Contents?: true

Size: 1.8 KB

Versions: 9

Compression:

Stored size: 1.8 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 = {})
      super
      @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)
      output
    end

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

    def response_info(env, response_time)
      format(
        'Response from %<method>s; Status: %<status>d; Time: %<time>.1fms',
        method: env[:method].to_s.upcase,
        status: env[:status],
        time: (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, _| k.to_s.size }.max
      headers.map { |name, value| "#{name.to_s.ljust(length)} : #{filter(value)}" }.join("\n")
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
firebase-admin-0.3.0 lib/faraday/loud_logger.rb
firebase-admin-0.2.0 lib/faraday/loud_logger.rb
firebase-admin-0.1.6 lib/faraday/loud_logger.rb
firebase-admin-0.1.5 lib/faraday/loud_logger.rb
firebase-admin-0.1.4 lib/faraday/loud_logger.rb
firebase-admin-0.1.3 lib/faraday/loud_logger.rb
firebase-admin-0.1.2 lib/faraday/loud_logger.rb
firebase-admin-0.1.1 lib/faraday/loud_logger.rb
firebase-admin-0.1.0 lib/faraday/loud_logger.rb