Skip to content

Commit 477cda4

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

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/fastapi_cli/cli.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from rich import print
88
from rich.tree import Tree
99
from typing_extensions import Annotated
10+
from uvicorn.config import LOGGING_CONFIG
1011

1112
from fastapi_cli.config import FastAPIConfig
1213
from fastapi_cli.discover import get_import_data, get_import_data_from_import_string
@@ -102,6 +103,7 @@ def _run(
102103
entrypoint: Union[str, None] = None,
103104
proxy_headers: bool = False,
104105
forwarded_allow_ips: Union[str, None] = None,
106+
log_config: Union[Path, None] = None,
105107
) -> None:
106108
with get_rich_toolkit() as toolkit:
107109
server_type = "development" if command == "dev" else "production"
@@ -214,7 +216,7 @@ def _run(
214216
root_path=root_path,
215217
proxy_headers=proxy_headers,
216218
forwarded_allow_ips=forwarded_allow_ips,
217-
log_config=get_uvicorn_log_config(),
219+
log_config=get_uvicorn_log_config() if not log_config else str(log_config),
218220
)
219221

220222

@@ -278,6 +280,12 @@ def dev(
278280
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
279281
),
280282
] = None,
283+
log_config: Annotated[
284+
Union[Path, None],
285+
typer.Option(
286+
help="Logging configuration file. Supported formats: .ini, .json, .yaml. be tried."
287+
),
288+
] = None,
281289
) -> Any:
282290
"""
283291
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
@@ -315,6 +323,7 @@ def dev(
315323
command="dev",
316324
proxy_headers=proxy_headers,
317325
forwarded_allow_ips=forwarded_allow_ips,
326+
log_config=log_config,
318327
)
319328

320329

@@ -384,6 +393,12 @@ def run(
384393
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
385394
),
386395
] = None,
396+
log_config: Annotated[
397+
Union[Path, None],
398+
typer.Option(
399+
help="Logging configuration file. Supported formats: .ini, .json, .yaml."
400+
),
401+
] = None,
387402
) -> Any:
388403
"""
389404
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
@@ -422,6 +437,7 @@ def run(
422437
command="run",
423438
proxy_headers=proxy_headers,
424439
forwarded_allow_ips=forwarded_allow_ips,
440+
log_config=log_config,
425441
)
426442

427443

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ def test_run_args() -> None:
295295
"--app",
296296
"api",
297297
"--no-proxy-headers",
298+
"--log-config",
299+
"log_config.yaml",
298300
],
299301
)
300302
assert result.exit_code == 0, result.output
@@ -407,6 +409,10 @@ def test_dev_help() -> None:
407409
assert "The root path is used to tell your app" in result.output
408410
assert "The name of the variable that contains the FastAPI app" in result.output
409411
assert "Use multiple worker processes." not in result.output
412+
assert (
413+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
414+
in result.output
415+
)
410416

411417

412418
def test_run_help() -> None:
@@ -428,6 +434,10 @@ def test_run_help() -> None:
428434
assert "The root path is used to tell your app" in result.output
429435
assert "The name of the variable that contains the FastAPI app" in result.output
430436
assert "Use multiple worker processes." in result.output
437+
assert (
438+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
439+
in result.output
440+
)
431441

432442

433443
def test_callback_help() -> None:

0 commit comments

Comments
 (0)