Skip to content

Conversation

@07pepa
Copy link

@07pepa 07pepa commented Jun 18, 2024

sometimes its nice to have your custom logging in fastapi so i added passing it to uvicorn and fallbacking to uvicorn default one if nothing is passed in

@07pepa 07pepa marked this pull request as draft June 18, 2024 16:11
@07pepa 07pepa force-pushed the add-log-config-overides branch from 9f35763 to 457b390 Compare June 19, 2024 07:47
@07pepa 07pepa marked this pull request as ready for review June 19, 2024 07:48
@Leon0824
Copy link

+1

2 similar comments
@FelixBehne
Copy link

+1

@wchaws
Copy link

wchaws commented Jul 29, 2024

+1

@saligrama
Copy link

+1

@07pepa 07pepa force-pushed the add-log-config-overides branch from 457b390 to 5ad2af6 Compare August 19, 2024 14:10
@07pepa
Copy link
Author

07pepa commented Aug 19, 2024

@wchaws can you please add tag feature to fix failing test? thanks

@wchaws
Copy link

wchaws commented Aug 20, 2024

@07pepa I'm not the maintainer for this project. I just accidentally approved this PR.

@07pepa
Copy link
Author

07pepa commented Aug 20, 2024

ok i thought you would have some extra rights if you could approve

patrick91 added a commit to patrick91/fastapi-cli that referenced this pull request Sep 23, 2024
…_existing_app

✨ Allow to use an existing app
@07pepa 07pepa force-pushed the add-log-config-overides branch from 5ad2af6 to fce4d85 Compare September 24, 2024 16:07
@ghost
Copy link

ghost commented Sep 29, 2024

+1

@jrs53
Copy link

jrs53 commented Oct 22, 2024

Is this PR just missing label "feature"? @07pepa @wchaws Are you able to add it?

Thanks

@07pepa 07pepa force-pushed the add-log-config-overides branch from fce4d85 to 14cb9ad Compare October 22, 2024 11:27
@07pepa
Copy link
Author

07pepa commented Oct 22, 2024

Is this PR just missing label "feature"? @07pepa @wchaws Are you able to add it?

Thanks

Yes i am unable as author of PR to change/add label. Label is only thing that is missing.

i tried even modifying labeler and no luck #110

we probably need to ask @tiangolo how to do this (or to add label)

@vitaliy-sk
Copy link

+1

@mapapa
Copy link

mapapa commented Jan 2, 2025

Hihi all, is anybody looking into fixing the labeler issue? Thanks

@07pepa 07pepa force-pushed the add-log-config-overides branch from 14cb9ad to 0961863 Compare January 4, 2025 23:19
@07pepa
Copy link
Author

07pepa commented Jan 10, 2025

@mapapa i can't

@LoopKarma
Copy link

Can you please help this feature to go through @tiangolo? it's very annoying to see unnecessary access logs in production servers

@darowny
Copy link

darowny commented Feb 6, 2025

+1 I need ECS logging for fastapi run

@07pepa 07pepa force-pushed the add-log-config-overides branch from 26404c4 to c1974b1 Compare March 7, 2025 08:16
@darowny
Copy link

darowny commented Mar 25, 2025

Any updates here ? Could I take over if I need this functionality ? What is the problem with labels ?

@07pepa
Copy link
Author

07pepa commented Mar 25, 2025

Any updates here ? Could I take over if I need this functionality ? What is the problem with labels ?

this is problem with labes
https://github.com/fastapi/fastapi-cli/actions/runs/13716548094/job/38362509674?pr=36

also problem here #160
probably we need acces to repo to add labels

@07pepa
Copy link
Author

07pepa commented Mar 25, 2025

@darowny you can take over

@Morry98
Copy link

Morry98 commented Apr 8, 2025

+1 i really need to change the uvicorn log level because some path like readiness and liveness in k8s are called every few seconds and the exception will "disappear" inside the logs after few minutes.
(also the possibility to have change the log only for specific paths should be nice. don't know if uvicorn support something similar or should be done in fastapi code)

@agbrettpittman
Copy link

+1 I also need this as the default logger doesn't have timestamps. When errors occur, it makes troubleshooting much more difficult. There are workarounds, but being able to specify a logging override would be the easiest method.

@sanderbollen-clockworks

I too would love to be able to override the logger - I'd like to be able to use the same formatter that's used for my application code. Especially the lack of timestamps for access logs is quite annoying.

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2025

This pull request has a merge conflict that needs to be resolved.

@07pepa 07pepa force-pushed the add-log-config-overides branch from c1974b1 to 98095ce Compare September 22, 2025 17:19
@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Sep 22, 2025
@07pepa 07pepa force-pushed the add-log-config-overides branch from 98095ce to 74093c6 Compare September 22, 2025 17:20
@CostcoFanboy
Copy link

CostcoFanboy commented Oct 12, 2025

+1, imo this is what stops this wrapper from being genuinely production-ready.

@Morry98
Copy link

Morry98 commented Oct 12, 2025

Following up on my previous comment saying that after some studies I will suggest for production service to avoid fastapi-cli installing fastapi and uvicorn without fastapi[standard] or other versions.

You can create the main.py file with explicit uvicorn configuration:

import uvicorn

uvicorn.run(
    "app_folder.app_creator:app",
    host=<host>,  # example: "0.0.0.0"
    port=<port>,  # example: 8080
    workers=<workers>,  # example: 3
    reload=<reload>,  # false in production
    log_config=<logging_config dict> # Optional
)
  • I suggest to use pydantic-settings to create a configuration object containing all the parameters in order to be flexible with different configuration sources having a type validation
  • logging_config dict follow the python logging standard configuration that can be found here. In this way you can have a clean cli that is much readable server-side and you can set up other handlers like jsonl file or more fancy handlers. In my case I set up a clean and minimal cli log without fancy lines and similar stuff, plus a jsonl like handler that split the file by level and rotate them, all the handlers managed by a queue handler that process them in a separated thread in order to avoid api performance issues.

In app_creator.py create your FastAPI app with all necessary components like lifespan, routers, middleware etc.
Something similar to:

from fastapi import FastAPI

app = FastAPI(
    title="Your API",
    # all needed parameters and configurations
)

# Add routers, middleware, exception handlers, etc.
app.include_router(your_router)

Copilot AI review requested due to automatic review settings November 27, 2025 17:05
@07pepa 07pepa force-pushed the add-log-config-overides branch from 252a50d to e78f460 Compare November 27, 2025 17:06
Copilot finished reviewing on behalf of 07pepa November 27, 2025 17:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for custom logging configuration in FastAPI CLI by allowing users to pass a --log-config option to both fastapi dev and fastapi run commands. The implementation properly falls back to uvicorn's default logging configuration when no custom config is provided.

Key changes:

  • Added --log-config parameter to dev and run CLI commands that accepts a Path to a logging configuration file
  • Updated _run function to accept and conditionally use custom log config, falling back to get_uvicorn_log_config() when not provided
  • Added comprehensive test coverage including argument validation and help text verification

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/fastapi_cli/cli.py Added log_config parameter to _run, dev, and run functions with conditional logic to use custom config or fall back to default
tests/test_cli.py Added test cases verifying --log-config argument is properly passed to uvicorn and help text is correctly displayed
tests/assets/log_config.yaml Added sample logging configuration file for testing purposes with standard Python logging dict config format

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

log_config: Annotated[
Union[Path, None],
typer.Option(
help="Logging configuration file. Supported formats: .ini, .json, .yaml. be tried."
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help text has incomplete/garbled text at the end: "be tried." should be removed. The correct help text is shown in the run function at line 398: "Logging configuration file. Supported formats: .ini, .json, .yaml."

Suggested change
help="Logging configuration file. Supported formats: .ini, .json, .yaml. be tried."
help="Logging configuration file. Supported formats: .ini, .json, .yaml."

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.