Any rubygem to log/stdout the remote webservice api calls by gems like httparty, faraday, httpclient, etc?
While using gems like httparty/faraday/etc.. that makes remote api calls, is there any gem that can intercept the request, especially the reque开发者_运维百科st parameters to log or show in stdout?
I really could use something like this, so I resorted to overriding ruby's Net::HTTP class to insert logging. Works well so far, although it's not very robust.
module Net
class HTTP
alias_method(:orig_request, :request) unless method_defined?(:orig_request)
alias_method(:orig_connect, :connect) unless method_defined?(:orig_connect)
def request(req, body = nil, &block)
Rails.logger.debug("Sending: #{req.method} http://#{@address}:#{@port}#{req.path}")
Rails.logger.debug("Body: #{req.body}") unless req.body.nil?
orig_request(req, body, &block)
end
def connect
Rails.logger.debug("Connecting: #{@address}")
orig_connect
end
end
end
This will give you log entries like this:
Connecting: www.flickr.com
Sending: GET http://www.flickr.com:80/photos/thilo/4301203/
If a more flexible version of this is of interest (configurable logger, log details, whatever), I might roll it into a gem if none exists (which I can't imagine :). But for simple debugging purposes this should suffice.
BTW I only tested this with ruby 1.9.2
EDIT
I expanded on this a bit and packaged it as a gem. Check it out at https://github.com/trusche/httplog. Logs to any ruby Logger instance. Works for me with ruby 1.9.2 from the ruby console and from within a Rails 3.1 app, and has a bunch of passing rspec tests. Hope it helps.
I have not been able to find such a gem, nor an easy way to turn such functionality on (at least in faraday).
Have you thought of using a standard TCP monitor like http://ws.apache.org/commons/tcpmon? It allows you to connect through it, so instead of connecting to the remote server, you connect to the tcp monitor (on an arbitrary port) which in turn connects to the remote server.
Httparty has a debug_mode which allows you to stream the debug data to a file or stdout.
require "rubygems"
require "httparty"
class Foobar
include HTTParty
# If you want to stream to stdout
#debug_output $stdout
# Log to a file
debug_output File.new("httparty.log", "w+")
get('http://twitter.com/statuses/public_timeline.json')
end
Another way is to to use a proxy that tunnels and logs the requests.
精彩评论