[Twisted-Python] Persuading Python's Logging to use twisted.logger

Jeffrey Ollie jeff at ocjtech.us
Mon May 9 15:54:34 MDT 2016


Here's a snippet that shows how I've handled it.  This example uses the new
style of logging in Twisted but would work just as well in the old style.
I'm not sure if there's a label that you can supply to the logging.Handler
to handle all messages but that's probably just a matter of digging into
the source code.

import sys
import logging

from twisted.logger import Logger
from twisted.logger import globalLogBeginner
from twisted.logger import textFileLogObserver

class TxLogHandler(logging.Handler):
    log = Logger()

    def __init__(self, label):
        self.label = label
        logging.Handler.__init__(self)
        self.level = logging.DEBUG

    def flush(self):
        pass

    def emit(self, record):
        try:
            msg = self.format(record)
            self.log.debug('{msg:}', msg = '\n'.join(map(lambda line: '{}:
{}'.format(self.label, line), msg.split('\n'))))
        except:
            self.handleError(record)

tx = TxLogHandler('FastAGI')
logger = logging.getLogger('FastAGI')
logger.addHandler(tx)
logger.setLevel(logging.DEBUG)

tx = TxLogHandler('AMI')
logger = logging.getLogger('AMI')
logger.addHandler(tx)
logger.setLevel(logging.DEBUG)

output = textFileLogObserver(sys.stderr)
globalLogBeginner.beginLoggingTo([output])



On Mon, May 9, 2016 at 3:50 PM, Daniel Sutcliffe <dansut at gmail.com> wrote:

> The project I am working on uses pymodbus which I am sure shares a
> fairly common attribute with many other modules of using Python's
> standard Logging mechanism - a very reasonable choice even for a
> module that supports Twisted, the library can also be used entirely
> synchronously and thus would not want a required dependency of
> Twisted.
>
> It struck me that it would be great to be able to redirect the
> standard logging library to use twisted.logger by some sort of 'Clever
> Monkey Patching' and that this may be a relatively common
> requirement... however after extensive searching, and asking on the
> pymodbus list, I can't find any evidence that such a thing has ever
> been attempted or discussed.
>
> The reverse mechanism of sending twisted.logger's output to the
> standard library is of course handled by the
> twisted.logger.STDLibLogObserver (and similar in twisted legacy
> logging) but the documentation for this even suggests why this is a
> bad idea: 'Warning: specific logging configurations (example: network)
> can lead to this observer blocking.' which seems to me why it would be
> better to attempt this the other way around...
>
> Am I crazy to even think this? is it just the rambling of
> Python/Twisted newb? Or is there something I'm missing that would make
> this impossible to do generically, and awkward to provide a vague
> recipe of how to do?
>
> I do appreciate that twisted.logger offers a more feature rich
> (structured) API and the Logging API would only be able to provide
> level and text but it would be better than loosing any possibly useful
> log messages from used modules in my mind.
>
> If anyone can enlighten me I would be most appreciative,
> Cheers
> /dan
> --
> Daniel Sutcliffe <dansut at gmail.com>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>



-- 
Jeff Ollie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20160509/351b8b8a/attachment-0002.html>


More information about the Twisted-Python mailing list