Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/fastapi_cli/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_utils_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down