diff --git a/src/fastapi_cli/utils/cli.py b/src/fastapi_cli/utils/cli.py index f6805e7c..dff93ee8 100644 --- a/src/fastapi_cli/utils/cli.py +++ b/src/fastapi_cli/utils/cli.py @@ -12,7 +12,12 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: self.toolkit = get_rich_toolkit() def formatMessage(self, record: logging.LogRecord) -> str: - return self.toolkit.print_as_string(record.getMessage(), tag=record.levelname) + message = record.getMessage() + result = self.toolkit.print_as_string(message, tag=record.levelname) + # Prepend newline to fix alignment after ^C is printed by the terminal + if message == "Shutting down": + result = "\n" + result + return result def get_uvicorn_log_config() -> Dict[str, Any]: diff --git a/tests/test_utils_cli.py b/tests/test_utils_cli.py index a2463123..422e515f 100644 --- a/tests/test_utils_cli.py +++ b/tests/test_utils_cli.py @@ -37,6 +37,25 @@ def test_custom_formatter() -> None: assert "200" in formatted +def test_custom_formatter_shutdown_prepends_newline() -> None: + formatter = CustomFormatter() + + record = logging.LogRecord( + name="uvicorn.error", + level=logging.INFO, + pathname="", + lineno=0, + msg="Shutting down", + args=(), + exc_info=None, + ) + + formatted = formatter.formatMessage(record) + + assert formatted.startswith("\n") + assert "Shutting down" in formatted + + def test_log_config_does_not_disable_existing_loggers( caplog: LogCaptureFixture, ) -> None: