How to log raw XML requests and responses in Suds


Wed 11 December 2019

Suppose you are using Suds and you want to log every raw XML request and response to a SOAP service. This can be useful to debug an integration with a SOAP web service.

You can write a MessagePlugin class for this, and use it in the construction of your client.

import logging
from xml.dom.minidom import parseString
from suds.client import Client
from suds.plugin import MessagePlugin

logger = logging.getLogger(__name__)

class SudsLoggerPlugin(MessagePlugin):

    def pretty_log(self, xml_content):
        try:
            dom = parseString(xml_content)
            logger.debug(dom.toprettyxml())
        except:
            logger.exception('Cannot log XML')

    def sending(self, context):
        self.pretty_log(context.envelope)

    def received(self, context):
        self.pretty_log(context.reply)

client = Client('your/wsdl/path/here?wsdl',
    plugins=[ SudsLoggerPlugin() ])

Share: