fastapi-cli icon indicating copy to clipboard operation
fastapi-cli copied to clipboard

add logging overriding

Open 07pepa opened this issue 1 year ago • 24 comments

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 avatar Jun 18 '24 16:06 07pepa

+1

Leon0824 avatar Jul 18 '24 10:07 Leon0824

+1

FelixBehne avatar Jul 22 '24 08:07 FelixBehne

+1

wchaws avatar Jul 29 '24 14:07 wchaws

+1

saligrama avatar Aug 07 '24 23:08 saligrama

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

07pepa avatar Aug 19 '24 14:08 07pepa

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

wchaws avatar Aug 20 '24 05:08 wchaws

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

07pepa avatar Aug 20 '24 06:08 07pepa

+1

ghost avatar Sep 29 '24 06:09 ghost

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

Thanks

jrs53 avatar Oct 22 '24 03:10 jrs53

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 https://github.com/fastapi/fastapi-cli/pull/110

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

07pepa avatar Oct 22 '24 11:10 07pepa

+1

vitaliy-sk avatar Nov 20 '24 22:11 vitaliy-sk

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

mapapa avatar Jan 02 '25 18:01 mapapa

@mapapa i can't

07pepa avatar Jan 10 '25 09:01 07pepa

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

LoopKarma avatar Jan 28 '25 13:01 LoopKarma

+1 I need ECS logging for fastapi run

darowny avatar Feb 06 '25 17:02 darowny

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

darowny avatar Mar 25 '25 10:03 darowny

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 https://github.com/fastapi/fastapi-cli/pull/160 probably we need acces to repo to add labels

07pepa avatar Mar 25 '25 10:03 07pepa

@darowny you can take over

07pepa avatar Mar 25 '25 10:03 07pepa

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

Morry98 avatar Apr 08 '25 11:04 Morry98

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

agbrettpittman avatar Apr 22 '25 12:04 agbrettpittman

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.

sanderbollen-clockworks avatar May 12 '25 09:05 sanderbollen-clockworks

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

github-actions[bot] avatar Sep 05 '25 08:09 github-actions[bot]

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

CostcoFanboy avatar Oct 12 '25 00:10 CostcoFanboy

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)

Morry98 avatar Oct 12 '25 18:10 Morry98