Skip to content

Commit

Permalink
🐛 Do not disable existing loggers (#132)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Arminio <[email protected]>
  • Loading branch information
kraftp and patrick91 authored Dec 15, 2024
1 parent bc08401 commit ffe012e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/fastapi_cli/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def formatMessage(self, record: logging.LogRecord) -> str:
def get_uvicorn_log_config() -> Dict[str, Any]:
return {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"()": CustomFormatter,
Expand Down
20 changes: 20 additions & 0 deletions tests/test_utils_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from fastapi_cli.utils.cli import CustomFormatter, get_uvicorn_log_config
from pytest import LogCaptureFixture


def test_get_uvicorn_config_uses_custom_formatter() -> None:
Expand Down Expand Up @@ -33,3 +34,22 @@ def test_custom_formatter() -> None:
assert "127.0.0.1" in formatted
assert "GET / HTTP/1.1" in formatted
assert "200" in formatted


def test_log_config_does_not_disable_existing_loggers(
caplog: LogCaptureFixture,
) -> None:
logger1 = logging.getLogger(__name__)
logger1.setLevel(logging.INFO)
logger1.info("Message before configuration")

logging.config.dictConfig(get_uvicorn_log_config())

logger2 = logging.getLogger(__name__)

logger1.info("Message after configuration from logger1") # Should not appear
logger2.info("Message from logger2")

assert "Message before configuration" in caplog.text
assert "Message after configuration from logger1" in caplog.text
assert "Message from logger2" in caplog.text

0 comments on commit ffe012e

Please sign in to comment.