Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from danielfone/patch-3

Added option to log request and response headers.
  • Loading branch information...
commit 175bc5805d4804c92b00a2af25ab6b66a1d1dd23 2 parents 97281cc + 4bed2df
Bogdan Gusiev bogdan authored
Showing with 6 additions and 0 deletions.
  1. +6 −0 lib/http_logger.rb
6 lib/http_logger.rb
View
@@ -7,6 +7,7 @@
# == Setup logger
#
# Net::HTTP.logger = Logger.new('/tmp/all.log')
+# Net::HTTP.log_headers = true
#
# == Do request
#
@@ -18,10 +19,12 @@
class Net::HTTP
class << self
+ attr_accessor :log_headers
attr_accessor :logger
attr_accessor :colorize
end
+ self.log_headers = false
self.colorize = true
@@ -36,9 +39,12 @@ def request(request, body = nil, &block)
url = "http#{"s" if self.use_ssl?}://#{self.address}:#{self.port}#{request.path}"
ofset = Time.now - time
log("HTTP #{request.method} (%0.2fms)" % (ofset * 1000), url)
+ request.each_capitalized { |k,v| log("HTTP request header", "#{k}: #{v}") } if self.class.log_headers
log("POST params", request.body) if request.is_a?(::Net::HTTP::Post)
+ log("PUT body", request.body) if request.is_a?(::Net::HTTP::Put)
if defined?(response) && response
log("Response status", "#{response.class} (#{response.code})")
+ response.each_capitalized { |k,v| log("HTTP response header", "#{k}: #{v}") } if self.class.log_headers
body = response.body
log("Response body", body) unless body.is_a?(Net::ReadAdapter)
end
Please sign in to comment.
Something went wrong with that request. Please try again.