Skip to content

Commit

Permalink
refactor(logging): refactor litestar.logging.standard to get correc…
Browse files Browse the repository at this point in the history
…t API docs
  • Loading branch information
jderrien committed Mar 18, 2024
1 parent 6b6e0e5 commit 7f7b2ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
55 changes: 26 additions & 29 deletions litestar/logging/standard.py
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]
4 changes: 1 addition & 3 deletions tests/unit/test_logging/test_logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
from litestar.exceptions import ImproperlyConfiguredException
from litestar.logging.config import LoggingConfig, _get_default_handlers, default_handlers, default_picologging_handlers
from litestar.logging.picologging import QueueListenerHandler as PicologgingQueueListenerHandler
from litestar.logging.standard import QueueListenerHandler as StandardQueueListenerHandler
from litestar.status_codes import HTTP_200_OK
from litestar.testing import create_test_client
from tests.helpers import cleanup_logging_impl

if sys.version_info < (3, 12, 0):
from litestar.logging.standard import QueueListenerHandler as StandardQueueListenerHandler

if TYPE_CHECKING:
from _pytest.capture import CaptureFixture

Expand Down

0 comments on commit 7f7b2ca

Please sign in to comment.