Skip to content

[Feature] add logging overriding #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ internal:
- '!README.md'
- '!src/**'
- '!pyproject.toml'

#if branch starts with feature/ then apply this label
feature:
- head-branch: ['^feature', 'feature']
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
Loading