-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(logging): refactor
litestar.logging.standard
to get correc…
…t API docs
- Loading branch information
Showing
2 changed files
with
27 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,45 @@ | ||
from __future__ import annotations | ||
|
||
import atexit | ||
import sys | ||
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: | ||
This handler won't work with Python >= 3.12 and ``logging.config.dictConfig``. It might be deprecated in the future, | ||
please use ``logging.QueueHandler`` instead. | ||
""" | ||
|
||
class LoggingQueueListener(QueueListener): | ||
"""Custom ``QueueListener`` which starts and stops the listening process.""" | ||
def __init__(self, handlers: list[Any] | None = None) -> None: | ||
"""Initialize `QueueListenerHandler`. | ||
def __init__(self, queue: Queue[LogRecord], *handlers: Handler, respect_handler_level: bool = False) -> None: | ||
"""Initialize ``LoggingQueueListener``. | ||
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 = 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