Skip to content

Commit 0961863

Browse files
07pepa07pepa
07pepa
authored andcommitted
add logging overriding
1 parent 9a47418 commit 0961863

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

Diff for: src/fastapi_cli/cli.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from rich import print
77
from rich.tree import Tree
88
from typing_extensions import Annotated
9+
from uvicorn.config import LOGGING_CONFIG
910

1011
from fastapi_cli.discover import get_import_data
1112
from fastapi_cli.exceptions import FastAPICLIException
@@ -86,6 +87,7 @@ def _run(
8687
command: str,
8788
app: Union[str, None] = None,
8889
proxy_headers: bool = False,
90+
log_config: Union[Path, None] = None,
8991
) -> None:
9092
with get_rich_toolkit() as toolkit:
9193
server_type = "development" if command == "dev" else "production"
@@ -167,7 +169,7 @@ def _run(
167169
workers=workers,
168170
root_path=root_path,
169171
proxy_headers=proxy_headers,
170-
log_config=get_uvicorn_log_config(),
172+
log_config=get_uvicorn_log_config() if not log_config else str(log_config),
171173
)
172174

173175

@@ -216,6 +218,12 @@ def dev(
216218
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
217219
),
218220
] = True,
221+
log_config: Annotated[
222+
Union[Path, None],
223+
typer.Option(
224+
help="Logging configuration file. Supported formats: .ini, .json, .yaml. be tried."
225+
),
226+
] = None,
219227
) -> Any:
220228
"""
221229
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
@@ -251,6 +259,7 @@ def dev(
251259
app=app,
252260
command="dev",
253261
proxy_headers=proxy_headers,
262+
log_config=log_config,
254263
)
255264

256265

@@ -305,6 +314,12 @@ def run(
305314
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
306315
),
307316
] = True,
317+
log_config: Annotated[
318+
Union[Path, None],
319+
typer.Option(
320+
help="Logging configuration file. Supported formats: .ini, .json, .yaml."
321+
),
322+
] = None,
308323
) -> Any:
309324
"""
310325
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
@@ -341,6 +356,7 @@ def run(
341356
app=app,
342357
command="run",
343358
proxy_headers=proxy_headers,
359+
log_config=log_config,
344360
)
345361

346362

Diff for: tests/assets/log_config.yaml

+34
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

Diff for: tests/test_cli.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def test_dev_args() -> None:
9494
"--app",
9595
"api",
9696
"--no-proxy-headers",
97+
"--log-config",
98+
"log_config.yaml",
9799
],
98100
)
99101
assert result.exit_code == 0, result.output
@@ -107,7 +109,7 @@ def test_dev_args() -> None:
107109
"workers": None,
108110
"root_path": "/api",
109111
"proxy_headers": False,
110-
"log_config": get_uvicorn_log_config(),
112+
"log_config": "log_config.yaml",
111113
}
112114
assert "Using import string: single_file_app:api" in result.output
113115
assert "Starting development server 🚀" in result.output
@@ -166,6 +168,8 @@ def test_run_args() -> None:
166168
"--app",
167169
"api",
168170
"--no-proxy-headers",
171+
"--log-config",
172+
"log_config.yaml",
169173
],
170174
)
171175
assert result.exit_code == 0, result.output
@@ -179,7 +183,7 @@ def test_run_args() -> None:
179183
"workers": 2,
180184
"root_path": "/api",
181185
"proxy_headers": False,
182-
"log_config": get_uvicorn_log_config(),
186+
"log_config": "log_config.yaml",
183187
}
184188

185189
assert "Using import string: single_file_app:api" in result.output
@@ -218,6 +222,10 @@ def test_dev_help() -> None:
218222
assert "The root path is used to tell your app" in result.output
219223
assert "The name of the variable that contains the FastAPI app" in result.output
220224
assert "Use multiple worker processes." not in result.output
225+
assert (
226+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
227+
in result.output
228+
)
221229

222230

223231
def test_run_help() -> None:
@@ -239,6 +247,10 @@ def test_run_help() -> None:
239247
assert "The root path is used to tell your app" in result.output
240248
assert "The name of the variable that contains the FastAPI app" in result.output
241249
assert "Use multiple worker processes." in result.output
250+
assert (
251+
"Logging configuration file. Supported formats: .ini, .json, .yaml."
252+
in result.output
253+
)
242254

243255

244256
def test_callback_help() -> None:

0 commit comments

Comments
 (0)