authentik/passbook/lib/log.py

38 lines
1.1 KiB
Python
Raw Normal View History

"""QueueListener that can be configured from logging.dictConfig"""
2019-04-13 15:04:48 +01:00
from atexit import register
from logging.config import ConvertingList
2019-04-13 15:04:48 +01:00
from logging.handlers import QueueHandler, QueueListener
from queue import Queue
def _resolve_handlers(_list):
"""Evaluates ConvertingList by iterating over it"""
if not isinstance(_list, ConvertingList):
return _list
2019-04-13 15:04:48 +01:00
# Indexing the list performs the evaluation.
return [_list[i] for i in range(len(_list))]
2019-04-13 15:04:48 +01:00
class QueueListenerHandler(QueueHandler):
"""QueueListener that can be configured from logging.dictConfig"""
2019-04-13 15:04:48 +01:00
def __init__(self, handlers, auto_run=True, queue=Queue(-1)):
2019-04-13 15:04:48 +01:00
super().__init__(queue)
handlers = _resolve_handlers(handlers)
self._listener = QueueListener(
self.queue,
*handlers,
respect_handler_level=True)
2019-04-13 15:04:48 +01:00
if auto_run:
self.start()
register(self.stop)
def start(self):
"""start background thread"""
2019-04-13 15:04:48 +01:00
self._listener.start()
def stop(self):
"""stop background thread"""
2019-04-13 15:04:48 +01:00
self._listener.stop()