Skip to content

Commit e78f460

Browse files
07pepa07pepa
authored andcommitted
add logging overriding
1 parent cc841c2 commit e78f460

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
@@ -102,6 +102,7 @@ def _run(
102102
entrypoint: Union[str, None] = None,
103103
proxy_headers: bool = False,
104104
forwarded_allow_ips: Union[str, None] = None,
105+
log_config: Union[Path, None] = None,
105106
) -> None:
106107
with get_rich_toolkit() as toolkit:
107108
server_type = "development" if command == "dev" else "production"
@@ -214,7 +215,7 @@ def _run(
214215
root_path=root_path,
215216
proxy_headers=proxy_headers,
216217
forwarded_allow_ips=forwarded_allow_ips,
217-
log_config=get_uvicorn_log_config(),
218+
log_config=get_uvicorn_log_config() if not log_config else str(log_config),
218219
)
219220

220221

@@ -278,6 +279,12 @@ def dev(
278279
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
279280
),
280281
] = None,
282+
log_config: Annotated[
283+
Union[Path, None],
284+
typer.Option(
285+
help="Logging configuration file. Supported formats: .ini, .json, .yaml. be tried."
286+
),
287+
] = None,
281288
) -> Any:
282289
"""
283290
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
@@ -315,6 +322,7 @@ def dev(
315322
command="dev",
316323
proxy_headers=proxy_headers,
317324
forwarded_allow_ips=forwarded_allow_ips,
325+
log_config=log_config,
318326
)
319327

320328

@@ -384,6 +392,12 @@ def run(
384392
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
385393
),
386394
] = None,
395+
log_config: Annotated[
396+
Union[Path, None],
397+
typer.Option(
398+
help="Logging configuration file. Supported formats: .ini, .json, .yaml."
399+
),
400+
] = None,
387401
) -> Any:
388402
"""
389403
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
@@ -422,6 +436,7 @@ def run(
422436
command="run",
423437
proxy_headers=proxy_headers,
424438
forwarded_allow_ips=forwarded_allow_ips,
439+
log_config=log_config,
425440
)
426441

427442

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
@@ -111,6 +111,8 @@ def test_dev_args() -> None:
111111
"--app",
112112
"api",
113113
"--no-proxy-headers",
114+
"--log-config",
115+
"log_config.yaml",
114116
],
115117
)
116118
assert result.exit_code == 0, result.output
@@ -125,7 +127,7 @@ def test_dev_args() -> None:
125127
"root_path": "/api",
126128
"proxy_headers": False,
127129
"forwarded_allow_ips": None,
128-
"log_config": get_uvicorn_log_config(),
130+
"log_config": "log_config.yaml",
129131
}
130132
assert "Using import string: single_file_app:api" in result.output
131133
assert "Starting development server 🚀" in result.output
@@ -295,6 +297,8 @@ def test_run_args() -> None:
295297
"--app",
296298
"api",
297299
"--no-proxy-headers",
300+
"--log-config",
301+
"log_config.yaml",
298302
],
299303
)
300304
assert result.exit_code == 0, result.output
@@ -309,7 +313,7 @@ def test_run_args() -> None:
309313
"root_path": "/api",
310314
"proxy_headers": False,
311315
"forwarded_allow_ips": None,
312-
"log_config": get_uvicorn_log_config(),
316+
"log_config": "log_config.yaml",
313317
}
314318

315319
assert "Using import string: single_file_app:api" in result.output
@@ -407,6 +411,10 @@ def test_dev_help() -> None:
407411
assert "The root path is used to tell your app" in result.output
408412
assert "The name of the variable that contains the FastAPI app" in result.output
409413
assert "Use multiple worker processes." not in result.output
414+
assert (
415+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
416+
in result.output
417+
)
410418

411419

412420
def test_run_help() -> None:
@@ -428,6 +436,10 @@ def test_run_help() -> None:
428436
assert "The root path is used to tell your app" in result.output
429437
assert "The name of the variable that contains the FastAPI app" in result.output
430438
assert "Use multiple worker processes." in result.output
439+
assert (
440+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
441+
in result.output
442+
)
431443

432444

433445
def test_callback_help() -> None:

0 commit comments

Comments
 (0)