Skip to content

Commit

Permalink
feat(logging): add param logging_module to LoggingConfig (#3578)
Browse files Browse the repository at this point in the history
  • Loading branch information
jderrien authored Jun 17, 2024
1 parent 12193b4 commit a1f5b3b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 112 deletions.
28 changes: 17 additions & 11 deletions litestar/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ def get_logger_placeholder(_: str | None = None) -> NoReturn:
)


def _get_default_handlers() -> dict[str, dict[str, Any]]:
def _get_default_logging_module() -> str:
if find_spec("picologging"):
return "picologging"
return "logging"


def _get_default_handlers(logging_module: str) -> dict[str, dict[str, Any]]:
"""Return the default logging handlers for the config.
Returns:
A dictionary of logging handlers
"""
if find_spec("picologging"):
if logging_module == "picologging":
return default_picologging_handlers
return default_handlers

Expand Down Expand Up @@ -179,12 +185,11 @@ def set_level(logger: Any, level: int) -> None:

@dataclass
class LoggingConfig(BaseLoggingConfig):
"""Configuration class for standard logging.
Notes:
- If 'picologging' is installed it will be used by default.
"""
"""Configuration class for standard logging."""

logging_module: str = field(default_factory=_get_default_logging_module)
"""Logging module. ``logging`` and ``picologging`` are supported. ``picologging`` will be used by default if
installed."""
version: Literal[1] = field(default=1)
"""The only valid value at present is 1."""
incremental: bool = field(default=False)
Expand Down Expand Up @@ -213,7 +218,7 @@ class LoggingConfig(BaseLoggingConfig):
.. _Formatter: https://docs.python.org/3/library/logging.html#formatter-objects
"""
handlers: dict[str, dict[str, Any]] = field(default_factory=_get_default_handlers)
handlers: dict[str, dict[str, Any]] = field(default_factory=dict)
"""A dict in which each key is a handler id and each value is a dict describing how to configure the
corresponding Handler_ instance. Two handlers are provided, ``console`` and ``queue_listener``.
Expand Down Expand Up @@ -253,10 +258,10 @@ def __post_init__(self) -> None:
self.formatters["standard"] = _get_default_formatters()["standard"]

if "console" not in self.handlers:
self.handlers["console"] = _get_default_handlers()["console"]
self.handlers["console"] = _get_default_handlers(self.logging_module)["console"]

if "queue_listener" not in self.handlers:
self.handlers["queue_listener"] = _get_default_handlers()["queue_listener"]
self.handlers["queue_listener"] = _get_default_handlers(self.logging_module)["queue_listener"]

if "litestar" not in self.loggers:
self.loggers["litestar"] = _get_default_loggers()["litestar"]
Expand All @@ -274,6 +279,7 @@ def configure(self) -> GetLogger:
"""

excluded_fields: set[str] = {
"logging_module",
"configure_root_logger",
"exception_logging_handler",
"log_exceptions",
Expand All @@ -284,7 +290,7 @@ def configure(self) -> GetLogger:
if not self.configure_root_logger:
excluded_fields.add("root")

if "picologging" in " ".join([handler["class"] for handler in self.handlers.values()]):
if self.logging_module == "picologging":
try:
from picologging import config, getLogger
except ImportError as e:
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from time_machine import travel

from litestar.logging import LoggingConfig
from litestar.logging.config import default_handlers as logging_default_handlers
from litestar.middleware.session import SessionMiddleware
from litestar.middleware.session.base import BaseSessionBackend
from litestar.middleware.session.client_side import ClientSideSessionBackend, CookieBackendConfig
Expand Down Expand Up @@ -306,7 +305,7 @@ def get_logger() -> GetLogger:
# due to the limitations of caplog we have to place this call here.
# we also have to allow propagation.
return LoggingConfig(
handlers=logging_default_handlers,
logging_module="logging",
loggers={
"litestar": {"level": "INFO", "handlers": ["queue_listener"], "propagate": True},
},
Expand Down
Loading

0 comments on commit a1f5b3b

Please sign in to comment.