[Base] Add custom log observer to handle twisted errors

For some reason errors are logged by twisted as
'Unhandled error in Deferred', but without a following
stacktrace. This can happen in a deferred callback that e.g.
raises an ImportError. Without an excplicit error handler for a
deferred to log such errors, finding the error can be very tricky.

Fix this by using a custom twisted.python.log.PythonLoggingObserver,
PythonLoggingObserver, that also logs the traceback in addition to
the error message.
This commit is contained in:
bendikro 2016-05-01 16:38:19 +02:00 committed by Calum Lind
parent 5826446509
commit 6adbd14bf8
1 changed files with 19 additions and 1 deletions

View File

@ -153,11 +153,29 @@ def setup_logger(level="error", filename=None, filemode="w", logrotate=None):
root_logger.addHandler(handler) root_logger.addHandler(handler)
root_logger.setLevel(level) root_logger.setLevel(level)
twisted_logging = PythonLoggingObserver("twisted") twisted_logging = TwistedLoggingObserver()
twisted_logging.start() twisted_logging.start()
logging.getLogger("twisted").setLevel(level) logging.getLogger("twisted").setLevel(level)
class TwistedLoggingObserver(PythonLoggingObserver):
def __init__(self):
PythonLoggingObserver.__init__(self, loggerName='twisted')
def emit(self, event_dict):
log = logging.getLogger(__name__)
try:
fmt = "%(log_namespace)s "
if event_dict.get("log_format", None):
fmt += event_dict["log_format"]
if event_dict["isError"] and "failure" in event_dict:
fmt += "\n%(failure)s "
getattr(LoggingLoggerClass, event_dict["log_level"].name)(log, fmt % (event_dict))
except (KeyError, AttributeError) as ex:
log.error("ERROR when logging twisted error: '%s'", ex)
def tweak_logging_levels(): def tweak_logging_levels():
"""This function allows tweaking the logging levels for all or some loggers. """This function allows tweaking the logging levels for all or some loggers.
This is mostly usefull for developing purposes hence the contents of the This is mostly usefull for developing purposes hence the contents of the