Sha256: bee39d34de2feb841c897cd9ef865023ada6b89ca559be24104241b8b0d31ed0

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module HTTP
  module Features
    # Log requests and responses. Request verb and uri, and Response status are
    # logged at `info`, and the headers and bodies of both are logged at
    # `debug`. Be sure to specify the logger when enabling the feature:
    #
    #    HTTP.use(logging: {logger: Logger.new(STDOUT)}).get("https://example.com/")
    #
    class Logging < Feature
      attr_reader :logger

      def initialize(logger: NullLogger.new)
        @logger = logger
      end

      def wrap_request(request)
        logger.info { "> #{request.verb.to_s.upcase} #{request.uri}" }
        logger.debug do
          headers = request.headers.map { |name, value| "#{name}: #{value}" }.join("\n")
          body = request.body.source

          headers + "\n\n" + body.to_s
        end
        request
      end

      def wrap_response(response)
        logger.info { "< #{response.status}" }
        logger.debug do
          headers = response.headers.map { |name, value| "#{name}: #{value}" }.join("\n")
          body = response.body.to_s

          headers + "\n\n" + body
        end
        response
      end

      class NullLogger
        %w[fatal error warn info debug].each do |level|
          define_method(level.to_sym) do |*_args|
            nil
          end

          define_method(:"#{level}?") do
            true
          end
        end
      end

      HTTP::Options.register_feature(:logging, self)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
http-4.4.1 lib/http/features/logging.rb
http-4.4.0 lib/http/features/logging.rb
http-5.0.0.pre3 lib/http/features/logging.rb
http-5.0.0.pre2 lib/http/features/logging.rb
http-4.3.0 lib/http/features/logging.rb
http-4.2.0 lib/http/features/logging.rb
http-4.1.1 lib/http/features/logging.rb
http-4.1.0 lib/http/features/logging.rb
http-4.0.5 lib/http/features/logging.rb
http-4.0.4 lib/http/features/logging.rb
http-5.0.0.pre lib/http/features/logging.rb
http-4.0.3 lib/http/features/logging.rb
http-4.0.2 lib/http/features/logging.rb
http-4.0.1 lib/http/features/logging.rb
http-4.0.0 lib/http/features/logging.rb