question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

logs with FastAPI and Uvicorn

See original GitHub issue

Hello,

Thanks for FastAPI, easy to use in my Python projects ! However, I have an issue with logs. In my Python project, I use :

app = FastAPI()
uvicorn.run(app, host="0.0.0.0", port=8000)

And when i test my app I get in the console :

INFO: Started server process...
INFO: Waiting for application startup
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press ...)
INFO: 127.0.0.1:41668 - "GET /app/?type=env HTTP/1.1" 304 Not Modified
...

This 5 lines are exactly what I expected. Now I want to have access to these logs because I want to re-format them.

So, if i am right, I have a logger managing :

INFO: Started server process...
INFO: Waiting for application startup
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press ...)

and a logger managing :

INFO: 127.0.0.1:41668 - "GET /app/?type=env HTTP/1.1" 304 Not Modified

Is it right ?

I have no problem to get access to the first, I can use :

log1 = logging.getLogger("uvicorn.error")

and then change the formatter, but, for the second I was expecting that :

log2 = logging.getLogger("uvicorn.access")

will do the same, but I am unable to change the formatter for the log :

INFO: 127.0.0.1:41668 - "GET /app/?type=env HTTP/1.1" 304 Not Modified

How I can do that in my Python 3.7 script with FastAPI 0.52.0 and Uvicorn 0.11.3 ? Thank you.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:19
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

48reactions
Jackoviccommented, Jun 4, 2020

Doing :

log_config = uvicorn.config.LOGGING_CONFIG
log_config["formatters"]["access"]["fmt"] = "%(asctime)s - %(levelname)s - %(message)s"
log_config["formatters"]["default"]["fmt"] = "%(asctime)s - %(levelname)s - %(message)s"
uvicorn.run(app, log_config=log_config)

is exactly what I was looking for ! Thank you dbanty.

38reactions
dbantycommented, Jun 3, 2020

This is probably a better question for uvicorn, but I’ll take a stab at answering it. Full disclaimer, this is very likely not the “right way” to do this in uvicorn but…

uvicorn.run takes in a log_config keyword arg which lets you configure the loggers. There is a default log config, you could grab that and override what you need to if the case is simple enough.

log_config = uvicorn.config.LOGGING_CONFIG
log_config["formatters"]["access"]["fmt"] = "%(asctime)s - %(levelname)s - %(message)s"
uvicorn.run(app, log_config=log_config)

The problem with just getting the logger and configuring it seems to be that when you call uvicorn.run, uvicorn applies their logger handlers/formatters. If this is after you tried to apply your own handler, yours would get overridden.

You could also try configuring your logging handlers after FastAPI starts up? Might have better luck as uvicorn won’t be overriding at that point.

@app.on_event("startup")
async def startup_event():
    logger = logging.getLogger("uvicorn.access")
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
    logger.addHandler(handler)

The way I have that adds a new handler, it won’t override the existing internal one. If that’s okay for you it might be a better approach.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Override Uvicorn Logger in FastAPI using Loguru
Generally it is very difficult to override default uvicorn logs. When one deploys an application using Uvicorn, this is how the default logs...
Read more >
How to Add Timestamp to FastAPI/Uvicorn Logs
In this tutorial, you will learn to configure Uvicorn and add timestamp to all the access and debug logs. Let's have a look...
Read more >
FastAPI Uvicorn logging in Production - Nucu Labs
Hello ‍♂️, Running a ⏩FastAPI ⏩ application in production is very easy and fast, but along the way some Uvicorn logs are lost....
Read more >
Query logs from FastAPI uvicorn - New Relic Explorers Hub
I have a web app built on FastAPI and unvicorn. I've installed the Python agent per NEW_RELIC_CONFIG_FILE=path/to/newrelic.ini ...
Read more >
Settings - Uvicorn
Defaults to the $WEB_CONCURRENCY environment variable if available, or 1. Logging¶. --log-config <path> - Logging configuration file. Options: dictConfig() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found