-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logging): queue_listener handler for Python >= 3.12 (#3185)
* fix(logging): queue_listener handler for Python >= 3.12 * Update litestar/logging/standard.py * Update litestar/logging/standard.py * Update litestar/logging/standard.py * refactor(logging): refactor `litestar.logging.standard` to get correct API docs --------- Co-authored-by: Jacob Coffee <[email protected]>
- Loading branch information
1 parent
e763cea
commit be9af3f
Showing
6 changed files
with
191 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
import atexit | ||
import sys | ||
from logging import StreamHandler | ||
from logging import Handler, LogRecord, StreamHandler | ||
from logging.handlers import QueueHandler, QueueListener | ||
from queue import Queue | ||
from typing import Any | ||
|
||
from litestar.logging._utils import resolve_handlers | ||
|
||
__all__ = ("QueueListenerHandler",) | ||
__all__ = ("LoggingQueueListener", "QueueListenerHandler") | ||
|
||
|
||
if sys.version_info < (3, 12): | ||
class LoggingQueueListener(QueueListener): | ||
"""Custom ``QueueListener`` which starts and stops the listening process.""" | ||
|
||
class QueueListenerHandler(QueueHandler): | ||
"""Configure queue listener and handler to support non-blocking logging configuration.""" | ||
def __init__(self, queue: Queue[LogRecord], *handlers: Handler, respect_handler_level: bool = False) -> None: | ||
"""Initialize ``LoggingQueueListener``. | ||
def __init__(self, handlers: list[Any] | None = None) -> None: | ||
"""Initialize `?QueueListenerHandler`. | ||
Args: | ||
queue: The queue to send messages to | ||
*handlers: A list of handlers which will handle entries placed on the queue | ||
respect_handler_level: If ``respect_handler_level`` is ``True``, a handler's level is respected (compared with the level for the message) when deciding whether to pass messages to that handler | ||
""" | ||
super().__init__(queue, *handlers, respect_handler_level=respect_handler_level) | ||
self.start() | ||
atexit.register(self.stop) | ||
|
||
Args: | ||
handlers: Optional 'ConvertingList' | ||
""" | ||
super().__init__(Queue(-1)) | ||
handlers = resolve_handlers(handlers) if handlers else [StreamHandler()] | ||
self.listener = QueueListener(self.queue, *handlers) | ||
self.listener.start() | ||
|
||
atexit.register(self.listener.stop) | ||
class QueueListenerHandler(QueueHandler): | ||
"""Configure queue listener and handler to support non-blocking logging configuration. | ||
else: | ||
QueueListenerHandler = QueueHandler | ||
.. caution:: | ||
This handler doesn't work with Python >= 3.12 and ``logging.config.dictConfig``. It might | ||
be deprecated in the future. Please use ``logging.QueueHandler`` instead. | ||
""" | ||
|
||
def __init__(self, handlers: list[Any] | None = None) -> None: | ||
"""Initialize ``QueueListenerHandler``. | ||
Args: | ||
handlers: Optional 'ConvertingList' | ||
""" | ||
super().__init__(Queue(-1)) | ||
handlers = resolve_handlers(handlers) if handlers else [StreamHandler()] | ||
self.listener = LoggingQueueListener(self.queue, *handlers) # type: ignore[arg-type] |
This file contains 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
This file contains 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
Oops, something went wrong.