Class: HTTP::Features::Logging

Inherits:
HTTP::Feature show all
Defined in:
lib/http/features/logging.rb

Overview

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.new(STDOUT)).get("https://example.com/")

Defined Under Namespace

Classes: NullLogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HTTP::Feature

#on_error

Constructor Details

#initialize(logger: NullLogger.new) ⇒ Logging

Returns a new instance of Logging.


28
29
30
# File 'lib/http/features/logging.rb', line 28

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.


26
27
28
# File 'lib/http/features/logging.rb', line 26

def logger
  @logger
end

Instance Method Details

#wrap_request(request) ⇒ Object


32
33
34
35
36
37
# File 'lib/http/features/logging.rb', line 32

def wrap_request(request)
  logger.info { "> #{request.verb.to_s.upcase} #{request.uri}" }
  logger.debug { "#{stringify_headers(request.headers)}\n\n#{request.body.source}" }

  request
end

#wrap_response(response) ⇒ Object


39
40
41
42
43
44
# File 'lib/http/features/logging.rb', line 39

def wrap_response(response)
  logger.info { "< #{response.status}" }
  logger.debug { "#{stringify_headers(response.headers)}\n\n#{response.body}" }

  response
end