From 940b3eb94323fd4b60c64c76ae6aceb688dd231a Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 13 Apr 2019 16:04:48 +0200 Subject: [PATCH] move logging to separate thread --- passbook/core/settings.py | 29 ++++++++++++++++++----------- passbook/lib/log.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 passbook/lib/log.py diff --git a/passbook/core/settings.py b/passbook/core/settings.py index d0d52f71c..971b1bcfa 100644 --- a/passbook/core/settings.py +++ b/passbook/core/settings.py @@ -239,8 +239,6 @@ if not DEBUG: STATIC_URL = '/static/' STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' -LOG_HANDLERS = ['console', 'syslog', 'file'] - with CONFIG.cd('log'): LOGGING = { 'version': 1, @@ -283,41 +281,50 @@ with CONFIG.cd('log'): 'formatter': 'verbose', 'filename': CONFIG.get('file'), }, + 'queue': { + 'level': CONFIG.get('level').get('console'), + 'class': 'passbook.lib.log.QueueListenerHandler', + 'handlers': [ + 'cfg://handlers.console', + # 'cfg://handlers.syslog', + 'cfg://handlers.file', + ], + } }, 'loggers': { 'passbook': { - 'handlers': LOG_HANDLERS, + 'handlers': ['queue'], 'level': 'DEBUG', 'propagate': True, }, 'django': { - 'handlers': LOG_HANDLERS, - 'level': 'INFO', + 'handlers': ['queue'], + 'level': 'DEBUG', 'propagate': True, }, 'tasks': { - 'handlers': LOG_HANDLERS, + 'handlers': ['queue'], 'level': 'DEBUG', 'propagate': True, }, 'cherrypy': { - 'handlers': LOG_HANDLERS, + 'handlers': ['queue'], 'level': 'DEBUG', 'propagate': True, }, 'oauthlib': { - 'handlers': LOG_HANDLERS, + 'handlers': ['queue'], 'level': 'DEBUG', 'propagate': True, }, 'oauth2_provider': { - 'handlers': LOG_HANDLERS, + 'handlers': ['queue'], 'level': 'DEBUG', 'propagate': True, }, 'daphne': { - 'handlers': LOG_HANDLERS, - 'level': 'INFO', + 'handlers': ['queue'], + 'level': 'DEBUG', 'propagate': True, } } diff --git a/passbook/lib/log.py b/passbook/lib/log.py new file mode 100644 index 000000000..b4a72f917 --- /dev/null +++ b/passbook/lib/log.py @@ -0,0 +1,38 @@ +from atexit import register +from logging.config import ConvertingDict, ConvertingList, valid_ident +from logging.handlers import QueueHandler, QueueListener +from queue import Queue + +from django.conf import settings + + +def _resolve_handlers(l): + # import pudb; pu.db + if not isinstance(l, ConvertingList): + return l + + # Indexing the list performs the evaluation. + return [l[i] for i in range(len(l))] + + +class QueueListenerHandler(QueueHandler): + + def __init__(self, handlers, respect_handler_level=False, auto_run=True, queue=Queue(-1)): + super().__init__(queue) + handlers = _resolve_handlers(handlers) + self._listener = QueueListener( + self.queue, + *handlers, + respect_handler_level=respect_handler_level) + if auto_run: + self.start() + register(self.stop) + + def start(self): + self._listener.start() + + def stop(self): + self._listener.stop() + + def emit(self, record): + return super().emit(record)