From 6adbd14bf882f5edd8205dd5853d647d8be8680f Mon Sep 17 00:00:00 2001 From: bendikro Date: Sun, 1 May 2016 16:38:19 +0200 Subject: [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. --- deluge/log.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'deluge/log.py') diff --git a/deluge/log.py b/deluge/log.py index 4e0b27e4e..4c2c9b620 100644 --- a/deluge/log.py +++ b/deluge/log.py @@ -153,11 +153,29 @@ def setup_logger(level="error", filename=None, filemode="w", logrotate=None): root_logger.addHandler(handler) root_logger.setLevel(level) - twisted_logging = PythonLoggingObserver("twisted") + twisted_logging = TwistedLoggingObserver() twisted_logging.start() 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(): """This function allows tweaking the logging levels for all or some loggers. This is mostly usefull for developing purposes hence the contents of the -- cgit