How do you log the URL ActiveResource uses?
Asked Answered
K

3

23

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 difficult. 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?

Kiser answered 8/9, 2010 at 1:19 Comment(0)
U
26

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.

Undersecretary answered 11/11, 2010 at 0:4 Comment(3)
If you're not using ActiveRecord, just use a separate logger: active_resource_logger = Logger.new('log/active_resource.log', 'daily'); active_resource_logger.level = Rails.env.dev? ? Logger::DEBUG : Logger::INFO; ActiveResource::Base.logger = active_resource_loggerProminence
I know this is super old, but the http_logger gem (github.com/railsware/http_logger) works great for this. You can see the HTTP request that gets sent to the ActiveResource site and any errors that it might spit back out at you when you set HttpLogger.log_request_body = trueMccormack
You can also do ActiveResource::Base.logger = Rails.logger if you aren't using ActiveRecord.Nelia
G
9

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.

Gynandromorph answered 14/10, 2011 at 20:15 Comment(0)
S
2

enter image description here

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
Stilu answered 16/8, 2018 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.