Created
January 28, 2024 18:43
-
-
Save victory-sokolov/fc38a20ce4eccf04e4b0886bcec5c8cf to your computer and use it in GitHub Desktop.
Async logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import logging | |
import logging.handlers | |
try: | |
# Python 3.7 and newer, fast reentrant implementation | |
# without task tracking (not needed for that when logging) | |
from queue import SimpleQueue as Queue | |
except ImportError: | |
from queue import Queue | |
from typing import List | |
class LocalQueueHandler(logging.handlers.QueueHandler): | |
def emit(self, record: logging.LogRecord) -> None: | |
# Removed the call to self.prepare(), handle task cancellation | |
try: | |
self.enqueue(record) | |
except asyncio.CancelledError: | |
raise | |
except Exception: | |
self.handleError(record) | |
def setup_logging_queue() -> None: | |
"""Move log handlers to a separate thread. | |
Replace handlers on the root logger with a LocalQueueHandler, | |
and start a logging.QueueListener holding the original | |
handlers. | |
""" | |
queue = Queue() | |
root = logging.getLogger() | |
handlers: List[logging.Handler] = [] | |
handler = LocalQueueHandler(queue) | |
root.addHandler(handler) | |
for h in root.handlers[:]: | |
if h is not handler: | |
root.removeHandler(h) | |
handlers.append(h) | |
listener = logging.handlers.QueueListener( | |
queue, *handlers, respect_handler_level=True | |
) | |
listener.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment