Last active
September 16, 2019 14:41
-
-
Save vishaltelangre/5133551 to your computer and use it in GitHub Desktop.
Logging Savon SOAP requests/responses in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------------------------------------- | |
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" | |
} | |
} | |
} | |
-------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I figured out from here http://savonrb.com/version2/globals.html that
So I figured out that I could use something like this (instead of the above savon monkey patch code):
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!