5

I'm using Savon gem in Ruby on Rails to communicate with a wsdl WS. Everything is working fine, but I want to log the request XML using a custom log, i.e. not Rails or Savon logger. My code looks something like this:

    response = self.client.request :add_order do
      soap.body = { 
        :indata => {
          "CustomerNo" => config[:kundnr],
          "Pwd" => config[:password],
          "OrderDate" => order["purchase_order_date"].strftime('%Y%m%d')
        }
      }
    end

Accessing the response is no problem, but what about the request? I need to be able to see what has been sent in my production environment by logging the XML to a DB-field.

Marcus W
  • 589
  • 6
  • 11

2 Answers2

5

Right now there's no easy way to get the request XML. But as you noticed, Savon logs each request and response to a specified logger. So if you're not changing the log level, you could use a custom Logger-like object that responds to :debug and store what gets logged.

module DBLogger
  def self.debug(message)
    p message  # replace with custom impl.
  end
end

Savon.configure do |config|
  config.logger = DBLogger
end

Hope that helps!

rubiii
  • 6,903
  • 2
  • 38
  • 51
  • Thx for the reply, I'll try that approach and get back! – Marcus W May 17 '12 at 09:29
  • 1
    Worked like a charm! Or kind of anyway :) I ended up overriding debug in my own logger, parsing the message with Nokogiri and logging relevant information in my own db-field. Thx for the help rubiii. – Marcus W May 22 '12 at 08:09
  • it's cool thank you but you need me add "info" method like "debug" – eayurt Oct 31 '14 at 12:30
  • Just a quick update. Defining a logger is now ver much easier by telling the Savon.client about it: http://savonrb.com/version2/globals.html (-> Logging) – awenkhh Dec 22 '15 at 18:11
4

This wasn't explicitly stated in the official answer, but I had to include a couple more options to get requests/responses to log properly. In my case, I just used Rails.logger instead of rolling my own:

client = Savon.client(
  wsdl: 'http://example.com?wsdl',
  log: true,
  logger: Rails.logger,
  log_level: :debug
)

client.call(:example_operation, message: {example: 'example'})

The log_level might default to debug, but there's no harm in being explicit :) But without the log: true option, you won't see the actual request/response bodies, only urls and statuses.

I'm using Savon 2.7.2 for reference.

Magne
  • 16,401
  • 10
  • 68
  • 88
Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50