Skip to content

Commit 44e471f

Browse files
committed
Tests for logger
1 parent e11a0fb commit 44e471f

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/fastapi_cli/utils/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
1212
self.toolkit = get_rich_toolkit()
1313

1414
def formatMessage(self, record: logging.LogRecord) -> str:
15-
return self.toolkit.print_as_string(record.message, tag=record.levelname)
15+
return self.toolkit.print_as_string(record.getMessage(), tag=record.levelname)
1616

1717

1818
def get_uvicorn_log_config() -> Dict[str, Any]:
@@ -68,6 +68,7 @@ def get_rich_toolkit() -> RichToolkit:
6868
"result": "grey85",
6969
"progress": "on #007166",
7070
"error": "red",
71+
"log.info": "black on blue",
7172
},
7273
)
7374

tests/test_utils_cli.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import logging
2+
3+
from fastapi_cli.utils.cli import CustomFormatter, get_uvicorn_log_config
4+
5+
6+
def test_get_uvicorn_config_uses_custom_formatter() -> None:
7+
config = get_uvicorn_log_config()
8+
9+
assert config["formatters"]["default"]["()"] is CustomFormatter
10+
assert config["formatters"]["access"]["()"] is CustomFormatter
11+
12+
13+
def test_custom_formatter() -> None:
14+
formatter = CustomFormatter()
15+
16+
record = logging.LogRecord(
17+
name="uvicorn.access",
18+
level=logging.INFO,
19+
pathname="",
20+
lineno=0,
21+
msg="%(client_addr)s - '%(request_line)s' %(status_code)s",
22+
args={
23+
"client_addr": "127.0.0.1",
24+
"request_line": "GET / HTTP/1.1",
25+
"status_code": 200,
26+
},
27+
exc_info=None,
28+
)
29+
30+
formatted = formatter.formatMessage(record)
31+
32+
assert "INFO" in formatted
33+
assert "127.0.0.1" in formatted
34+
assert "GET / HTTP/1.1" in formatted
35+
assert "200" in formatted

0 commit comments

Comments
 (0)