From 44213083695aa9a53932db2b2f2d3613485c234e Mon Sep 17 00:00:00 2001 From: Lucas Uyezu Date: Thu, 6 Mar 2014 16:54:10 -0300 Subject: [PATCH 1/2] Redirects and final responses handles Content-Length differently --- lib/httparty/logger/apache_logger.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/httparty/logger/apache_logger.rb b/lib/httparty/logger/apache_logger.rb index 7561fef..b18b37b 100644 --- a/lib/httparty/logger/apache_logger.rb +++ b/lib/httparty/logger/apache_logger.rb @@ -14,7 +14,7 @@ module HTTParty current_time = Time.now.strftime("%Y-%m-%d %H:%M:%S %z") http_method = request.http_method.name.split("::").last.upcase path = request.path.to_s - content_length = response['Content-Length'] + content_length = response.respond_to?(:headers) ? response.headers['Content-Length'] : response['Content-Length'] @logger.send @level, "[#{TAG_NAME}] [#{current_time}] #{response.code} \"#{http_method} #{path}\" #{content_length || "-"} " end end From 1069df23c6730ab943425aae3ec4a9c5546ee939 Mon Sep 17 00:00:00 2001 From: Lucas Uyezu Date: Thu, 6 Mar 2014 17:18:45 -0300 Subject: [PATCH 2/2] Logging examples --- examples/logging.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/logging.rb diff --git a/examples/logging.rb b/examples/logging.rb new file mode 100644 index 0000000..362e568 --- /dev/null +++ b/examples/logging.rb @@ -0,0 +1,38 @@ +dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) +require File.join(dir, 'httparty') +require 'logger' +require 'pp' + +my_logger = Logger.new "httparty.log" + +my_logger.info "Logging can be used on the main HTTParty class. It logs redirects too." +HTTParty.get "http://google.com", logger: my_logger + +my_logger.info '*'*70 + +my_logger.info "It can be used also on a custom class." + +class Google + include HTTParty + logger ::Logger.new "httparty.log" +end + +Google.get "http://google.com" + +my_logger.info '*'*70 + +my_logger.info "The default formatter is :apache. The :curl formatter can also be used." +my_logger.info "You can tell wich method to call on the logger too. It is info by default." +HTTParty.get "http://google.com", logger: my_logger, log_level: :debug, log_format: :curl + + +my_logger.info '*'*70 + +my_logger.info "These configs are also available on custom classes." +class Google + include HTTParty + logger ::Logger.new("httparty.log"), :debug, :curl +end + +Google.get "http://google.com" +