Sha256: 5edf775391b94f506d9e4ef4986c82d53cbb262e4677a0aceec74aefaebe281e

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

module Spyke
  module Middleware
    class RailsLogger < Faraday::Middleware
      CLEAR   = "\e[0m"
      BOLD    = "\e[1m"
      MAGENTA = "\e[35m"

      def call(env)
        logger.formatter = -> (severity, datetime, progname, msg) { msg }

        logger.debug "\n\n\n\n#{env[:method].upcase} #{env[:url]}"
        logger.debug "\n  Headers: #{env[:request_headers]}"
        logger.debug "\n  Body: #{env[:body]}" if env[:body]

        @app.call(env).on_complete do
          Rails.logger.debug "  #{BOLD}#{MAGENTA}#{env[:method].upcase} #{env[:url]} [#{env[:status]}]#{CLEAR}"
          logger.debug "\n\nCompleted #{env[:status]}"
          logger.debug "\n  Headers: #{env[:response_headers]}"
          logger.debug "\n  Body: #{truncate_binary_values env[:body]}" if env[:body]
        end
      end

      private

        def logger
          @logger ||= Logger.new Rails.root.join('log', 'faraday.log')
        end

        def truncate_binary_values(body)
          body.gsub(/(data:)([^"]+)/, 'data:...')
        end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spyke-1.4.1 lib/spyke/middleware/rails_logger.rb
spyke-1.4.0 lib/spyke/middleware/rails_logger.rb
spyke-1.3.0 lib/spyke/middleware/rails_logger.rb
spyke-1.2.1 lib/spyke/middleware/rails_logger.rb
spyke-1.2.0 lib/spyke/middleware/rails_logger.rb
spyke-1.1.2 lib/spyke/middleware/rails_logger.rb
spyke-1.1.1 lib/spyke/middleware/rails_logger.rb
spyke-1.1.0 lib/spyke/middleware/rails_logger.rb