Skip to content

Instantly share code, notes, and snippets.

@vishaltelangre
Last active September 16, 2019 14:41
Show Gist options
  • Save vishaltelangre/5133551 to your computer and use it in GitHub Desktop.
Save vishaltelangre/5133551 to your computer and use it in GitHub Desktop.
Logging Savon SOAP requests/responses in Rails
# initializers/savon.rb
# Savon Global configuration
Savon.configure do |config|
config.log = true
config.log_level = :debug
config.logger = Rails.logger
config.env_namespace = :soapenv
end
HTTPI.log = false
# Log SOAP requests/responses in pretty JSON instead of XML
class Savon::SOAP::Request
def log_request(url, headers, body)
log_calling_line
SAVON_LOGGER.info "\n###### REQUEST:"
SAVON_LOGGER.info "URL: #{url}"
SAVON_LOGGER.info "Headers: #{headers}"
log_soap_prettily body
end
def log_response(code, body)
SAVON_LOGGER.info "\n###### RESPONSE:"
SAVON_LOGGER.info "Status Code: #{code}"
log_soap_prettily body
SAVON_LOGGER.info "-"*50 + "\n\n"
end
def log_calling_line
calling_line = caller[1..-1].find { |line| line.include?(Rails.root.to_s) }
return unless calling_line
calling_line.sub!(Rails.root.to_s + "/", "")
file, line, _ = calling_line.split(":")
code_line = File.readlines(file)[line.to_i - 1].strip
SAVON_LOGGER.info "\n\n" + "-"*50
SAVON_LOGGER.info "SOAP request started at #{Time.now}"
SAVON_LOGGER.info calling_line
SAVON_LOGGER.info "# #{code_line}"
end
def log_soap_prettily(soap)
hash = Hash.from_xml(soap)
body = hash["Envelope"]["Body"]
json = JSON.pretty_generate(body)
SAVON_LOGGER.info json
end
end
--------------------------------------------------
SOAP request started at 2013-03-11 13:34:41 +0530
app/models/some_web_service.rb:55:in `block (2 levels) in <class:SomeWebService>'
# response = send(WSDL[2]).request(:ns ,"#{WSDL[2]}_#{action}_request") do |soap, wsdl, http|
###### REQUEST:
URL: https://webserice.provider.com/services/some-service
Headers: {"charset"=>"UTF-8", "Connection"=>"Keep-Alive", "SOAPAction"=>"http://webserice.provider.com/some_kind_of_request/update", "Content-Type"=>"text/xml;charset=UTF-8", "Content-Length"=>"833"}
{
"some_kind_of_request_update": {
"identification": {
"email": "[email protected]",
"password": "12345"
}
}
}
###### RESPONSE:
Status Code: 200
{
"some_kind_of_response_update": {
"xmlns": "https://webserice.provider.com/services/some-service/types/1.6",
"identification": {
"id": "blah-blah",
"secret": "blah-blah",
"email": "[email protected]",
"password": "12345"
},
"status": {
"code": "200",
"message": "OK"
}
}
}
--------------------------------------------------
# lib/savon_logger.rb
class SavonLogger < Logger; end
logdir = "#{Rails.root}/log/savon"
Dir.mkdir(logdir) unless File.exists?(logdir)
logfile = File.open("#{logdir}/#{Time.now.to_date.to_s}.log", 'a')
logfile.sync = true # automatically flushes data to file
SAVON_LOGGER = SavonLogger.new(logfile)
# require this in desired environment file
@redbar0n
Copy link

redbar0n commented Dec 1, 2017

This looks great!

However I get:
NoMethodError: undefined method `configure' for Savon:Module

Because:
Savon has moved to version 2 and Savon.configure is no longer supported
Ref: http://savonrb.com/version2/globals.html

Any solution?

@redbar0n
Copy link

redbar0n commented Dec 2, 2017

I figured out from here http://savonrb.com/version2/globals.html that

Although they are called "global options", they really are local to a client instance. Savon version 1 was based on a global Savon.configure method to store the configuration. While this was a popular concept back then, adapted by tons of libraries, its problem is global state. I tried to fix that problem.

So I figured out that I could use something like this (instead of the above savon monkey patch code):

  client = Savon.client(
      wsdl: variable_with_wsdl_url,
      log: true,
      log_level: :debug,
      logger: Logger.new('log/savon.log', 10, 1024000)
    )

The logging output in Savon 2 isn't that bad, so I'll go with that instead of your code. But thanks for sharing though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment