Skip to content

Commit 74093c6

Browse files
07pepa07pepa
authored andcommitted
add logging overriding
1 parent 7d048df commit 74093c6

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/fastapi_cli/cli.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def _run(
100100
entrypoint: Union[str, None] = None,
101101
proxy_headers: bool = False,
102102
forwarded_allow_ips: Union[str, None] = None,
103+
log_config: Union[Path, None] = None,
103104
) -> None:
104105
with get_rich_toolkit() as toolkit:
105106
server_type = "development" if command == "dev" else "production"
@@ -186,7 +187,7 @@ def _run(
186187
root_path=root_path,
187188
proxy_headers=proxy_headers,
188189
forwarded_allow_ips=forwarded_allow_ips,
189-
log_config=get_uvicorn_log_config(),
190+
log_config=get_uvicorn_log_config() if not log_config else str(log_config),
190191
)
191192

192193

@@ -250,6 +251,12 @@ def dev(
250251
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
251252
),
252253
] = None,
254+
log_config: Annotated[
255+
Union[Path, None],
256+
typer.Option(
257+
help="Logging configuration file. Supported formats: .ini, .json, .yaml. be tried."
258+
),
259+
] = None,
253260
) -> Any:
254261
"""
255262
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
@@ -287,6 +294,7 @@ def dev(
287294
command="dev",
288295
proxy_headers=proxy_headers,
289296
forwarded_allow_ips=forwarded_allow_ips,
297+
log_config=log_config,
290298
)
291299

292300

@@ -356,6 +364,12 @@ def run(
356364
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
357365
),
358366
] = None,
367+
log_config: Annotated[
368+
Union[Path, None],
369+
typer.Option(
370+
help="Logging configuration file. Supported formats: .ini, .json, .yaml."
371+
),
372+
] = None,
359373
) -> Any:
360374
"""
361375
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
@@ -394,6 +408,7 @@ def run(
394408
command="run",
395409
proxy_headers=proxy_headers,
396410
forwarded_allow_ips=forwarded_allow_ips,
411+
log_config=log_config,
397412
)
398413

399414

tests/assets/log_config.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: 1
2+
disable_existing_loggers: False
3+
formatters:
4+
default:
5+
# "()": uvicorn.logging.DefaultFormatter
6+
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
7+
access:
8+
# "()": uvicorn.logging.AccessFormatter
9+
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
10+
handlers:
11+
default:
12+
formatter: default
13+
class: logging.StreamHandler
14+
stream: ext://sys.stderr
15+
access:
16+
formatter: access
17+
class: logging.StreamHandler
18+
stream: ext://sys.stdout
19+
loggers:
20+
uvicorn.error:
21+
level: DEBUG
22+
handlers:
23+
- default
24+
propagate: no
25+
uvicorn.access:
26+
level: DEBUG
27+
handlers:
28+
- access
29+
propagate: no
30+
root:
31+
level: INFO
32+
handlers:
33+
- default
34+
propagate: no

tests/test_cli.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def test_dev_args() -> None:
9696
"--app",
9797
"api",
9898
"--no-proxy-headers",
99+
"--log-config",
100+
"log_config.yaml",
99101
],
100102
)
101103
assert result.exit_code == 0, result.output
@@ -110,7 +112,7 @@ def test_dev_args() -> None:
110112
"root_path": "/api",
111113
"proxy_headers": False,
112114
"forwarded_allow_ips": None,
113-
"log_config": get_uvicorn_log_config(),
115+
"log_config": "log_config.yaml",
114116
}
115117
assert "Using import string: single_file_app:api" in result.output
116118
assert "Starting development server 🚀" in result.output
@@ -263,6 +265,8 @@ def test_run_args() -> None:
263265
"--app",
264266
"api",
265267
"--no-proxy-headers",
268+
"--log-config",
269+
"log_config.yaml",
266270
],
267271
)
268272
assert result.exit_code == 0, result.output
@@ -277,7 +281,7 @@ def test_run_args() -> None:
277281
"root_path": "/api",
278282
"proxy_headers": False,
279283
"forwarded_allow_ips": None,
280-
"log_config": get_uvicorn_log_config(),
284+
"log_config": "log_config.yaml",
281285
}
282286

283287
assert "Using import string: single_file_app:api" in result.output
@@ -375,6 +379,10 @@ def test_dev_help() -> None:
375379
assert "The root path is used to tell your app" in result.output
376380
assert "The name of the variable that contains the FastAPI app" in result.output
377381
assert "Use multiple worker processes." not in result.output
382+
assert (
383+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
384+
in result.output
385+
)
378386

379387

380388
def test_run_help() -> None:
@@ -396,6 +404,10 @@ def test_run_help() -> None:
396404
assert "The root path is used to tell your app" in result.output
397405
assert "The name of the variable that contains the FastAPI app" in result.output
398406
assert "Use multiple worker processes." in result.output
407+
assert (
408+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
409+
in result.output
410+
)
399411

400412

401413
def test_callback_help() -> None:

0 commit comments

Comments
 (0)