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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With