Skip to content

Commit

Permalink
add logging overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
07pepa committed Aug 19, 2024
1 parent c925ac9 commit 5ad2af6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/fastapi_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rich.padding import Padding
from rich.panel import Panel
from typing_extensions import Annotated
from uvicorn.config import LOGGING_CONFIG

from fastapi_cli.discover import get_import_string
from fastapi_cli.exceptions import FastAPICLIException
Expand Down Expand Up @@ -60,6 +61,7 @@ def _run(
command: str,
app: Union[str, None] = None,
proxy_headers: bool = False,
log_config: Union[Path, None] = None,
) -> None:
try:
use_uvicorn_app = get_import_string(path=path, app_name=app)
Expand Down Expand Up @@ -89,6 +91,7 @@ def _run(
raise FastAPICLIException(
"Could not import Uvicorn, try running 'pip install uvicorn'"
) from None

uvicorn.run(
app=use_uvicorn_app,
host=host,
Expand All @@ -97,6 +100,8 @@ def _run(
workers=workers,
root_path=root_path,
proxy_headers=proxy_headers,
# fallback to default uvicorn log config if nothing is provided
log_config=LOGGING_CONFIG if not log_config else str(log_config),
)


Expand Down Expand Up @@ -145,6 +150,12 @@ def dev(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
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. 🧪
Expand Down Expand Up @@ -180,6 +191,7 @@ def dev(
app=app,
command="dev",
proxy_headers=proxy_headers,
log_config=log_config,
)


Expand Down Expand Up @@ -234,6 +246,12 @@ def run(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
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. 🚀
Expand Down Expand Up @@ -270,6 +288,7 @@ def run(
app=app,
command="run",
proxy_headers=proxy_headers,
log_config=log_config,
)


Expand Down
34 changes: 34 additions & 0 deletions tests/assets/log_config.yaml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uvicorn
from fastapi_cli.cli import app
from typer.testing import CliRunner
from uvicorn.config import LOGGING_CONFIG

from tests.utils import changing_dir

Expand All @@ -29,6 +30,7 @@ def test_dev() -> None:
"workers": None,
"root_path": "",
"proxy_headers": True,
"log_config": LOGGING_CONFIG,
}
assert "Using import string single_file_app:app" in result.output
assert (
Expand Down Expand Up @@ -58,6 +60,8 @@ def test_dev_args() -> None:
"--app",
"api",
"--no-proxy-headers",
"--log-config",
"log_config.yaml",
],
)
assert result.exit_code == 0, result.output
Expand All @@ -71,6 +75,7 @@ def test_dev_args() -> None:
"workers": None,
"root_path": "/api",
"proxy_headers": False,
"log_config": "log_config.yaml",
}
assert "Using import string single_file_app:api" in result.output
assert (
Expand All @@ -97,6 +102,7 @@ def test_run() -> None:
"workers": None,
"root_path": "",
"proxy_headers": True,
"log_config": LOGGING_CONFIG,
}
assert "Using import string single_file_app:app" in result.output
assert (
Expand Down Expand Up @@ -128,6 +134,8 @@ def test_run_args() -> None:
"--app",
"api",
"--no-proxy-headers",
"--log-config",
"log_config.yaml",
],
)
assert result.exit_code == 0, result.output
Expand All @@ -141,6 +149,7 @@ def test_run_args() -> None:
"workers": 2,
"root_path": "/api",
"proxy_headers": False,
"log_config": "log_config.yaml",
}
assert "Using import string single_file_app:api" in result.output
assert (
Expand Down Expand Up @@ -178,6 +187,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:
Expand All @@ -199,6 +212,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:
Expand Down

0 comments on commit 5ad2af6

Please sign in to comment.