开发者

How do you log the URL ActiveResource uses?

Rails ActiveResource is awesome ... except for one thing: as far as I can tell, there is no way to see what URL it is using behind the scenes. For instance, let's say I have an ActiveResource called Issue, for a webservice at myIssues.com/issues.xml. If I do:

Issue.find(:all, :params => {:page => 2})

I would expect that ActiveResource would make a call to:

myIssues.com/issues.xml?page=2

... but I don't actually know that. For all I know, ActiveResource could have decided it doesn't like the word "page", so it's actually using:

myIssues.com/issues.xml?mod_page=2

This makes debugging dif开发者_如何转开发ficult. Right now I've got a situation where, if I go to the URL I think ActiveResource is using, it works just fine. However, when I actually use ActiveResource, it doesn't work. Seeing the URL it's GETing would be immensely helpful in this, so ...

Does anyone know a way to log (or otherwise output; if there's some resource.url method that would work great too) the URL(s) that ActiveResource uses to do its thing?


If you add the following line to your environment.rb file, it will at least log the requests so you know that URLs ActiveResource is hitting:

ActiveResource::Base.logger = ActiveRecord::Base.logger

I'm still searching for a better solution that shows me the response and the data posted to update calls, but at least this is a step in the right direction. I'm really not sure why ActiveResource has a separate logger to start with, but that's another matter.


I just ran into this same exact issue, and came across this post as I was looking for answers. What I did find, that proved useful, is the collection_path method on ActiveResource::Base. So for example, let's say you have the following resource:

class UserPost < ActiveResource::Base
    self.site = "http://someApp.com/user/:user_id"
    self.element_name = "post"

If you go to the rails console, here are some examples of the output:

>> UserPost.collection_path
"/user//post"
>> UserPost.collection_path(:user_id => 5)
"/user/5/post

This should provide you with exactly what you need to determine how ActiveResource is translating your request into a URL.


How do you log the URL ActiveResource uses?

To get detail login for ActiveResource have to patch the request method inside the gem(method.

place bellow files inside config/initializers you will get http method, path, request body, request hedaers

response body and header is already there if you need. doc

config/initializers/activeresource_patch.rb

module ActiveResource
  class Connection
    private
      def request(method, path, *arguments)
        result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
          payload[:method]      = method
          payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
          payload[:request_path] = path
          payload[:request_body] = arguments[0]
          payload[:request_headers] = arguments[1]
          payload[:result]      = http.send(method, path, *arguments)
        end
        handle_response(result)
      rescue Timeout::Error => e
        raise TimeoutError.new(e.message)
      rescue OpenSSL::SSL::SSLError => e
        raise SSLError.new(e.message)
      end
  end
end

config/initializers/activeresource_logger.rb

Rails.application.configure do

  def activeresource_logger
  @activeresource_logger ||= Logger.new("#{Rails.root}/log/activeresource_logger.log")
  end

  ActiveSupport::Notifications.subscribe('request.active_resource')  do |name, start, finish, id, payload|
   if Rails.env.development?
    activeresource_logger.info("====================== #{start} : #{payload[:method].upcase} ======================")
    activeresource_logger.info("PATH: #{payload[:request_path]}")
    activeresource_logger.info("BODY: #{payload[:request_body]}")
    activeresource_logger.info("HEADERS: #{payload[:request_headers]}")
    # activeresource_logger.info("STATUS_CODE: #{payload[:result].code}")
    # activeresource_logger.info("RESPONSE_BODY: #{payload[:result].body}")
   end
  end

end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜