Logging outgoing soap messages

IT Igor Támara Igor Támara

Igor Támara

Senior Software Engineer
1 min read.

When you have to fiddle with ye old SOAP services and you use python, certainly someone has made tons of work for you with the suds library, chances are that you are forced to log to show that there is something strange in the endpoint given that usually it is like black magic(for the people behind it that just push the play button but most of the time are not involved in what a rest service is, because are behind an IDE that makes the tricks for them).

We show you how we made it log to a file.   By the way, if you master the Python logging module, you have plenty of knowledge as a Python programmer.  Join in :)


Suds tends to log things, so we have to "capture" the logging part we are interested 
suds.transport.http, we define our outgoing messages using the function OutgoingFilter knowing that suds uses the sending: string.

import logging
LOG_FORMAT = u": %(asctime)s - %(name)s - %(levelname)s - %(message)s"
LOGFILE = '/tmp/outgoing_fedex.log'
class OutgoingFilter(logging.Filter):
    """We only need to filter outgoing messages
    """
    def filter(self, record):
        return record.msg.startswith('sending:')


Now we are ready to capture the logging

def activate_suds_logger(action):
    """Log to LOGFILE outgoing messages from suds.transport.http logger
    """
    loggersuds = logging.getLogger('suds.transport.http')
    handler = logging.handlers.RotatingFileHandler(
        LOGFILE, maxBytes=(1048576*5), backupCount=7
    )
    formatter = logging.Formatter(
        action + LOG_FORMAT
    )
    handler.setFormatter(formatter)
    loggersuds.setLevel(logging.DEBUG), handler.setLevel(logging.DEBUG)
    handler.addFilter(OutgoingFilter())
    loggersuds.addHandler(handler)


Please remember to invoke the functions so it actually starts to log close to the suds call with something like:

   activate_suds_logger(u'MY LOGGING CHUNK')


If you want to know which loggers are active at the moment you can use the handy

logging.Logger.manager.loggerDict.keys()

References


Written by Igor Támara

IT Igor Támara Igor Támara

A seasoned developer, Igor brings expertise in designing and building complex software systems. With a focus on quality and performance, they lead projects that drive innovation and deliver reliable solutions to meet user needs.

Newsletter

Subscribe to our newsletter:

Read more

Build Once. Own Forever.