Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure logger and force "DEBUG" level in debug mode #1119

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions starlite/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -65,6 +66,7 @@
BeforeMessageSendHookHandler,
BeforeRequestHookHandler,
ControllerRouterHandler,
EmptyType,
ExceptionHandlersMap,
Guard,
LifeSpanHandler,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 [],
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions starlite/config/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def validate_loggers( # pylint: disable=no-self-argument
value["starlite"] = {
"level": "INFO",
"handlers": ["queue_listener"],
"propagate": False,
}
return value

Expand Down
22 changes: 22 additions & 0 deletions tests/app/test_app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]