Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Implements option for Savon to log tidy xml #280

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions lib/savon/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def log_level
end

# Logs a given +message+. Optionally filtered if +xml+ is truthy.
def log(message, xml = false)
def log(message, message_type = false)
return unless log?
message = filter_xml(message) if xml && !log_filter.empty?
message = process_xml(message) if message_type = :xml
logger.send log_level, message
end

Expand All @@ -44,15 +44,11 @@ def log_filter
# Sets the log filter. Expects an Array.
attr_writer :log_filter

# Filters the given +xml+ based on log filter.
# TODO - filter_xml should be moved to the Savon class
# Accepts a string, parses the string, filters it according to Savon.log_filter, then returns a string
def filter_xml(xml)
doc = Nokogiri::XML(xml)
return xml unless doc.errors.empty?

log_filter.each do |filter|
doc.xpath("//*[local-name()='#{filter}']").map { |node| node.content = "***FILTERED***" }
end

filter_xml_doc!(doc)
doc.root.to_s
end

Expand Down Expand Up @@ -96,8 +92,39 @@ def reset_config!
self.soap_version = nil
self.env_namespace = nil
self.soap_header = nil
self.pretty_xml_logs = false
end

# When true, tidy all Savon xml log output
attr_writer :pretty_xml_logs

def pretty_xml_logs?
@pretty_xml_logs ||= false
end

private

# TODO - process_xml and filter_xml_doc! should be moved to the Savon class
def process_xml(message)
return message if log_filter.empty? && ! pretty_xml_logs?
doc = Nokogiri::XML(message)
filter_xml_doc!(doc) unless log_filter.empty?
if pretty_xml_logs?
doc.to_xml(:indent => 2)
else
# TODO - is there an option for Nokogiri::XML#to_xml to return the xml all as one line?
doc.to_xml(:indent => 0).gsub("\n", "")
end
end

def filter_xml_doc!(doc)
return unless doc.errors.empty?

log_filter.each do |filter|
doc.xpath("//*[local-name()='#{filter}']").map { |node| node.content = "***FILTERED***" }
end
true
end
end
end

4 changes: 2 additions & 2 deletions lib/savon/soap/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def with_logging
def log_request(url, headers, body)
Savon.log "SOAP request: #{url}"
Savon.log headers.map { |key, value| "#{key}: #{value}" }.join(", ")
Savon.log body, :filter
Savon.log body, :xml
end

# Logs the SOAP response +code+ and +body+.
def log_response(code, body)
Savon.log "SOAP response (status #{code}):"
Savon.log body
Savon.log body, :xml
end

end
Expand Down
15 changes: 14 additions & 1 deletion spec/savon/savon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,20 @@
msg.include?('<ns11:logType>***FILTERED***</ns11:logType>')
end

Savon.log(Fixture.response(:list), :filter)
Savon.log(Fixture.response(:list), :xml)
end
end

context "pretty_xml_logs is set" do
it "returns formatted xml with indentation" do
Savon.configure { |config| config.pretty_xml_logs = true }
Savon.logger.expects(Savon.log_level).with <<-EOF
<?xml version="1.0"?>
<hello>
<world>Bob</world>
</hello>
EOF
Savon.log("<?xml version=\"1.0\"?><hello><world>Bob</world></hello>", :xml)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/savon/soap/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
HTTPI.stubs(:post).returns(HTTPI::Response.new 200, {}, "")

Savon.stubs(:log).times(2)
Savon.expects(:log).with(soap.to_xml, :filter)
Savon.expects(:log).with(soap.to_xml, :xml)
Savon.stubs(:log).times(2)

soap_request.response
Expand Down