From e78f460006cbbcaaa797de6868bdc838baa4ea34 Mon Sep 17 00:00:00 2001 From: 07pepa Date: Tue, 18 Jun 2024 18:09:50 +0200 Subject: [PATCH] add logging overriding --- src/fastapi_cli/cli.py | 17 ++++++++++++++++- tests/assets/log_config.yaml | 34 ++++++++++++++++++++++++++++++++++ tests/test_cli.py | 16 ++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 tests/assets/log_config.yaml diff --git a/src/fastapi_cli/cli.py b/src/fastapi_cli/cli.py index c8180b01..d39d262c 100644 --- a/src/fastapi_cli/cli.py +++ b/src/fastapi_cli/cli.py @@ -102,6 +102,7 @@ def _run( entrypoint: Union[str, None] = None, proxy_headers: bool = False, forwarded_allow_ips: Union[str, None] = None, + log_config: Union[Path, None] = None, ) -> None: with get_rich_toolkit() as toolkit: server_type = "development" if command == "dev" else "production" @@ -214,7 +215,7 @@ def _run( root_path=root_path, proxy_headers=proxy_headers, forwarded_allow_ips=forwarded_allow_ips, - log_config=get_uvicorn_log_config(), + log_config=get_uvicorn_log_config() if not log_config else str(log_config), ) @@ -278,6 +279,12 @@ def dev( help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything." ), ] = None, + log_config: Annotated[ + Union[Path, None], + typer.Option( + help="Logging configuration file. Supported formats: .ini, .json, .yaml. be tried." + ), + ] = None, ) -> Any: """ Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. ๐Ÿงช @@ -315,6 +322,7 @@ def dev( command="dev", proxy_headers=proxy_headers, forwarded_allow_ips=forwarded_allow_ips, + log_config=log_config, ) @@ -384,6 +392,12 @@ def run( help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything." ), ] = None, + log_config: Annotated[ + Union[Path, None], + typer.Option( + help="Logging configuration file. Supported formats: .ini, .json, .yaml." + ), + ] = None, ) -> Any: """ Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. ๐Ÿš€ @@ -422,6 +436,7 @@ def run( command="run", proxy_headers=proxy_headers, forwarded_allow_ips=forwarded_allow_ips, + log_config=log_config, ) diff --git a/tests/assets/log_config.yaml b/tests/assets/log_config.yaml new file mode 100644 index 00000000..1678d761 --- /dev/null +++ b/tests/assets/log_config.yaml @@ -0,0 +1,34 @@ +version: 1 +disable_existing_loggers: False +formatters: + default: + # "()": uvicorn.logging.DefaultFormatter + format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + access: + # "()": uvicorn.logging.AccessFormatter + format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' +handlers: + default: + formatter: default + class: logging.StreamHandler + stream: ext://sys.stderr + access: + formatter: access + class: logging.StreamHandler + stream: ext://sys.stdout +loggers: + uvicorn.error: + level: DEBUG + handlers: + - default + propagate: no + uvicorn.access: + level: DEBUG + handlers: + - access + propagate: no +root: + level: INFO + handlers: + - default + propagate: no diff --git a/tests/test_cli.py b/tests/test_cli.py index 0a0d7ab1..08087940 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -111,6 +111,8 @@ def test_dev_args() -> None: "--app", "api", "--no-proxy-headers", + "--log-config", + "log_config.yaml", ], ) assert result.exit_code == 0, result.output @@ -125,7 +127,7 @@ def test_dev_args() -> None: "root_path": "/api", "proxy_headers": False, "forwarded_allow_ips": None, - "log_config": get_uvicorn_log_config(), + "log_config": "log_config.yaml", } assert "Using import string: single_file_app:api" in result.output assert "Starting development server ๐Ÿš€" in result.output @@ -295,6 +297,8 @@ def test_run_args() -> None: "--app", "api", "--no-proxy-headers", + "--log-config", + "log_config.yaml", ], ) assert result.exit_code == 0, result.output @@ -309,7 +313,7 @@ def test_run_args() -> None: "root_path": "/api", "proxy_headers": False, "forwarded_allow_ips": None, - "log_config": get_uvicorn_log_config(), + "log_config": "log_config.yaml", } assert "Using import string: single_file_app:api" in result.output @@ -407,6 +411,10 @@ def test_dev_help() -> None: assert "The root path is used to tell your app" in result.output assert "The name of the variable that contains the FastAPI app" in result.output assert "Use multiple worker processes." not in result.output + assert ( + "Logging configuration file. Supported formats: .ini, .json, .yaml." + in result.output + ) def test_run_help() -> None: @@ -428,6 +436,10 @@ def test_run_help() -> None: assert "The root path is used to tell your app" in result.output assert "The name of the variable that contains the FastAPI app" in result.output assert "Use multiple worker processes." in result.output + assert ( + "Logging configuration file. Supported formats: .ini, .json, .yaml." + in result.output + ) def test_callback_help() -> None: