suds-community / suds

Suds is a lightweight SOAP python client for consuming Web Services. A community fork of the jurko fork.
https://suds.readthedocs.io/
GNU Lesser General Public License v3.0
172 stars 54 forks source link

Better debug log output... #17

Open jedie opened 5 years ago

jedie commented 5 years ago

I have made a simple log handler for 'suds.transport' that will print the binary output a little bit better to the console.

The normal output:

grafik

With this hacked handler:

grafik

The source code is here: https://github.com/django-oscar/django-oscar-docdata/pull/33

But it's a better place to include this into the suds project, isn't it?

Any interest here? Then I would try to make an pull request.

phillbaker commented 5 years ago

@jedie thanks for opening an issue! Given Python's structure of separating logging handlers from loggers and the specific constants in the handler you wrote (e.g. HIDE_SETTING_NAMES), I'm not sure that specific logginghandler makes sense in this library.

What do you think about documenting how to build similar custom handlers in the readme? e.g. adding a section similar to:

class SudsLogHandler(logging.StreamHandler):
    def emit(self, record):
        try:
            msg = self.format(record)

            # Note: We get the "suds.transport" log output as strings.
            # e.g.: MESSAGE is only a string representation of bytes, 
            # so reformat newlines:
            msg = msg.replace("\\n", "\n")

            # Evaluate specific attributes
            for attr in ['foo', 'bar']:
                attr_value = getattr(settings, attr, None)
                if attr_value:
                    msg = msg.replace(settings_value, "***")

            # Do the output with some newlines:
            stream = self.stream
            stream.write(self.terminator)
            stream.write(msg)
            stream.write(self.terminator)
            self.flush()
        except Exception:
            self.handleError(record)

import sys, logging
logger = logging.getLogger('suds.transport')
logger.setLevel(logging.DEBUG)
logger.addHandler(SudsLogHandler(sys.stdout))
jedie commented 5 years ago

The first question would be if there is any interest at all in including this in the project. Then we could see if we could improve it ;)

One question would be, is there a way to get the raw bytes in the log handler? Because there is only the string representation of bytes and we must handle escapes...

HIDE_SETTING_NAMES

This is from django settings. It's just about filtering username and password. It doesn't make sense to solve this in the same way here.

Maybe we can just add a list of "sensible values" as log handler arguments?