From 01c9d39af2407ced62cba36e4c740f7dfc2ba398 Mon Sep 17 00:00:00 2001 From: provinzkraut <25355197+provinzkraut@users.noreply.github.com> Date: Tue, 31 Jan 2023 14:43:30 +0100 Subject: [PATCH] Configure logger and force "DEBUG" level in debug mode --- starlite/app.py | 11 ++++++++--- starlite/config/logging.py | 1 + tests/app/test_app_config.py | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/starlite/app.py b/starlite/app.py index f0d29ced10..0794c8fd10 100644 --- a/starlite/app.py +++ b/starlite/app.py @@ -20,7 +20,7 @@ from starlite.asgi import ASGIRouter from starlite.asgi.utils import get_route_handlers, wrap_in_exception_handler from starlite.config import AllowedHostsConfig, AppConfig, CacheConfig, OpenAPIConfig -from starlite.config.logging import get_logger_placeholder +from starlite.config.logging import LoggingConfig, get_logger_placeholder from starlite.connection import Request, WebSocket from starlite.datastructures.state import ImmutableState, State from starlite.exceptions import ( @@ -33,6 +33,7 @@ from starlite.router import Router from starlite.routes import ASGIRoute, HTTPRoute, WebSocketRoute from starlite.signature import create_signature_model +from starlite.types import Empty from starlite.types.internal_types import PathParameterDefinition from starlite.utils import ( as_async_callable_list, @@ -65,6 +66,7 @@ BeforeMessageSendHookHandler, BeforeRequestHookHandler, ControllerRouterHandler, + EmptyType, ExceptionHandlersMap, Guard, LifeSpanHandler, @@ -181,7 +183,7 @@ def __init__( exception_handlers: Optional["ExceptionHandlersMap"] = None, guards: Optional[List["Guard"]] = None, initial_state: Optional[Union["ImmutableState", Dict[str, Any], Iterable[Tuple[str, Any]]]] = None, - logging_config: Optional["BaseLoggingConfig"] = None, + logging_config: Union["BaseLoggingConfig", "EmptyType", None] = Empty, middleware: Optional[List["Middleware"]] = None, on_app_init: Optional[List["OnAppInitHandler"]] = None, on_shutdown: Optional[List["LifeSpanHandler"]] = None, @@ -306,7 +308,7 @@ def __init__( etag=etag, exception_handlers=exception_handlers or {}, guards=guards or [], - logging_config=logging_config, + logging_config=logging_config if logging_config is not Empty else LoggingConfig() if debug else None, # type: ignore[arg-type] middleware=middleware or [], on_shutdown=on_shutdown or [], on_startup=on_startup or [], @@ -379,6 +381,9 @@ def __init__( for route_handler in config.route_handlers: self.register(route_handler) + if self.debug and isinstance(self.logging_config, LoggingConfig): + self.logging_config.loggers["starlite"]["level"] = "DEBUG" + if self.logging_config: self.get_logger = self.logging_config.configure() self.logger = self.get_logger("starlite") diff --git a/starlite/config/logging.py b/starlite/config/logging.py index 08a14abbba..42f4e07bcc 100644 --- a/starlite/config/logging.py +++ b/starlite/config/logging.py @@ -174,6 +174,7 @@ def validate_loggers( # pylint: disable=no-self-argument value["starlite"] = { "level": "INFO", "handlers": ["queue_listener"], + "propagate": False, } return value diff --git a/tests/app/test_app_config.py b/tests/app/test_app_config.py index be18b68cd3..4f96fb6da6 100644 --- a/tests/app/test_app_config.py +++ b/tests/app/test_app_config.py @@ -4,6 +4,7 @@ import pytest +from starlite import LoggingConfig from starlite.app import DEFAULT_CACHE_CONFIG, Starlite from starlite.config.app import AppConfig from starlite.router import Router @@ -99,3 +100,24 @@ def test_app_config_object_used(app_config_object: AppConfig, monkeypatch: pytes # this ensures that each of the properties of the `AppConfig` object have been accessed within `Starlite.__init__()` for mock in property_mocks: mock.assert_called() + + +def test_app_debug_create_logger() -> None: + app = Starlite([], debug=True) + + assert app.logging_config + assert app.logging_config.loggers["starlite"]["level"] == "DEBUG" # type: ignore[attr-defined] + + +def test_app_debug_explicitly_disable_logging() -> None: + app = Starlite([], debug=True, logging_config=None) + + assert not app.logging_config + + +def test_app_debug_update_logging_config() -> None: + logging_config = LoggingConfig() + app = Starlite([], debug=True, logging_config=logging_config) + + assert app.logging_config is logging_config + assert app.logging_config.loggers["starlite"]["level"] == "DEBUG" # type: ignore[attr-defined]