-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added initial logging support. #243
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e885c18
Added initial logging support.
lucasuyezu e0a96f8
Logging request body
lucasuyezu f0bfbcd
Testing regexp must consider timezones with '+' and '-'.
lucasuyezu c0fe182
Ruby 1.8 does not support %L on Time#strftime.
lucasuyezu 9934d6d
Removing memoization
lucasuyezu 45c9b55
Exposing raw_body instead of raw_request.
lucasuyezu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module HTTParty | ||
module Logger | ||
class ApacheLogger #:nodoc: | ||
TAG_NAME = HTTParty.name | ||
|
||
attr_accessor :level, :logger, :current_time | ||
|
||
def initialize(logger, level) | ||
@logger = logger | ||
@level = level.to_sym | ||
end | ||
|
||
def format(request, response) | ||
@current_time ||= Time.new.strftime("%Y-%m-%d %H:%M:%S.%L %z") | ||
http_method = request.http_method.name.split("::").last.upcase | ||
path = request.path.to_s | ||
content_length = response['Content-Length'] | ||
|
||
print(response.code, content_length, http_method, path) | ||
end | ||
|
||
def print(code, content_length, http_method, path) | ||
@logger.send @level, "[#{TAG_NAME}] [#{@current_time}] #{code} \"#{http_method} #{path}\" #{content_length || "-"} " | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module HTTParty | ||
module Logger | ||
class CurlLogger #:nodoc: | ||
TAG_NAME = HTTParty.name | ||
|
||
attr_accessor :level, :logger, :current_time | ||
|
||
def initialize(logger, level) | ||
@logger = logger | ||
@level = level.to_sym | ||
end | ||
|
||
def format(request, response) | ||
@messages = [] | ||
@current_time ||= Time.new.strftime("%Y-%m-%d %H:%M:%S %z") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note as apache logger. |
||
http_method = request.http_method.name.split("::").last.upcase | ||
path = request.path.to_s | ||
|
||
print_outgoing "#{http_method} #{path}" | ||
if request.options[:headers] && request.options[:headers].size > 0 | ||
request.options[:headers].each do |k, v| | ||
print_outgoing "#{k}: #{v}" | ||
end | ||
end | ||
|
||
print_outgoing request.raw_request.body if request.raw_request && request.raw_request.body | ||
print_outgoing "" | ||
print_incoming "HTTP/#{response.http_version} #{response.code}" | ||
|
||
headers = response.respond_to?(:headers) ? response.headers : response | ||
response.each_header do |response_header| | ||
print_incoming "#{response_header.capitalize}: #{headers[response_header]}" | ||
end | ||
|
||
print_incoming "\n#{response.body}" | ||
|
||
@logger.send @level, @messages.join("\n") | ||
end | ||
|
||
def print_outgoing(line) | ||
@messages << print(">", line) | ||
end | ||
|
||
def print_incoming(line) | ||
@messages << print("<", line) | ||
end | ||
|
||
def print(direction, line) | ||
"[#{TAG_NAME}] [#{@current_time}] #{direction} #{line}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'httparty/logger/apache_logger' | ||
require 'httparty/logger/curl_logger' | ||
|
||
module HTTParty | ||
module Logger | ||
def self.build(logger, level, formatter) | ||
level ||= :info | ||
format ||= :apache | ||
|
||
case formatter | ||
when :curl | ||
Logger::CurlLogger.new(logger, level) | ||
else | ||
Logger::ApacheLogger.new(logger, level) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ class Request #:nodoc: | |
end | ||
|
||
attr_accessor :http_method, :options, :last_response, :redirect, :last_uri | ||
attr_reader :path | ||
attr_reader :path, :raw_request | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather expose raw_body than raw_request. That is the only thing you are using, correct? |
||
|
||
def initialize(http_method, path, o={}) | ||
self.http_method = http_method | ||
|
@@ -245,6 +245,10 @@ def encode_body(body) | |
def handle_response(body, &block) | ||
if response_redirects? | ||
options[:limit] -= 1 | ||
if options[:logger] | ||
logger = HTTParty::Logger.build(options[:logger], options[:log_level], options[:log_format]) | ||
logger.format(self, last_response) | ||
end | ||
self.path = last_response['location'] | ||
self.redirect = true | ||
self.http_method = Net::HTTP::Get unless options[:maintain_method_across_redirects] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) | ||
|
||
describe HTTParty::Logger::ApacheLogger do | ||
describe "#format" do | ||
it "formats a response in a style that resembles apache's access log" do | ||
request_time = Time.new.strftime("%Y-%m-%d %H:%M:%S.%L %z") | ||
log_message = "[HTTParty] [#{request_time}] 302 \"GET http://my.domain.com/my_path\" - " | ||
|
||
request_double = double( | ||
:http_method => Net::HTTP::Get, | ||
:path => "http://my.domain.com/my_path" | ||
) | ||
response_double = double( | ||
:code => 302, | ||
:[] => nil | ||
) | ||
|
||
logger_double = double | ||
logger_double.should_receive(:info).with(log_message) | ||
|
||
subject = described_class.new(logger_double, :info) | ||
subject.current_time = request_time | ||
subject.format(request_double, response_double) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) | ||
|
||
describe HTTParty::Logger::CurlLogger do | ||
describe "#format" do | ||
it "formats a response in a style that resembles a -v curl" do | ||
logger_double = double | ||
logger_double.should_receive(:info).with( | ||
/\[HTTParty\] \[\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\ [+-]\d{4}\] > GET http:\/\/localhost/) | ||
|
||
subject = described_class.new(logger_double, :info) | ||
|
||
stub_http_response_with("google.html") | ||
|
||
response = HTTParty::Request.new.perform | ||
subject.format(response.request, response) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) | ||
|
||
describe HTTParty::Logger do | ||
describe ".build" do | ||
subject { HTTParty::Logger } | ||
|
||
it "defaults level to :info" do | ||
logger_double = double() | ||
subject.build(logger_double, nil, nil).level.should == :info | ||
end | ||
|
||
it "defaults format to :apache" do | ||
logger_double = double() | ||
subject.build(logger_double, nil, nil).should be_an_instance_of(HTTParty::Logger::ApacheLogger) | ||
end | ||
|
||
it "builds :curl style logger" do | ||
logger_double = double() | ||
subject.build(logger_double, nil, :curl).should be_an_instance_of(HTTParty::Logger::CurlLogger) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this make it always use the same time?