Class: HttpLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/http_logger.rb

Overview

Usage:

require 'http_logger'

Setup logger

HttpLogger.logger = Logger.new('/tmp/all.log')
HttpLogger.log_headers = true

Do request

res = Net::HTTP.start(url.host, url.port) { |http|
  http.request(req)
}
...

View the log

cat /tmp/all.log

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.collapse_body_limitObject

Returns the value of attribute collapse_body_limit.



26
27
28
# File 'lib/http_logger.rb', line 26

def collapse_body_limit
  @collapse_body_limit
end

.colorizeObject

Returns the value of attribute colorize.



29
30
31
# File 'lib/http_logger.rb', line 29

def colorize
  @colorize
end

.ignoreObject

Returns the value of attribute ignore.



30
31
32
# File 'lib/http_logger.rb', line 30

def ignore
  @ignore
end

.log_headersObject

Returns the value of attribute log_headers.



27
28
29
# File 'lib/http_logger.rb', line 27

def log_headers
  @log_headers
end

.loggerObject

Returns the value of attribute logger.



28
29
30
# File 'lib/http_logger.rb', line 28

def logger
  @logger
end

Class Method Details

.deprecate_config(option) ⇒ Object



46
47
48
# File 'lib/http_logger.rb', line 46

def self.deprecate_config(option)
  warn "Net::HTTP.#{option} is deprecated. Use HttpLogger.#{option} instead."
end

.instanceObject



42
43
44
# File 'lib/http_logger.rb', line 42

def self.instance
  @instance ||= HttpLogger.new
end

.perform(*args, &block) ⇒ Object



38
39
40
# File 'lib/http_logger.rb', line 38

def self.perform(*args, &block)
  instance.perform(*args, &block)
end

Instance Method Details

#perform(http, request, request_body) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/http_logger.rb', line 50

def perform(http, request, request_body)
  start_time = Time.now
  response = yield
ensure
  if require_logging?(http, request)
    log_request_url(http, request, start_time)
    log_request_body(request)
    log_request_headers(request)
    if defined?(response) && response
      log_response_code(response)
      log_response_headers(response)
      log_response_body(response.body)
    end
  end
end