I am running a simple FastAPI application under uvicorn. The FastAPI code is this:
from fastapi import FastAPI
@app.post("/events")
def create_events():
print("entering create_events()")
raise Exception("an error")
I run the app:
uvicorn api.main:app --reload --log-level=debug
I now post to the endpoint using wget:
wget -O- --header='Content-Type:application/json' --post-file=/tmp/data.json http://127.0.0.1:8000/events
Not surprisingly, the wget returns a 500 Internal Server Error.
In the output of the terminal where I ran uvicorn I see this:
entering create_events()
In other web application contexts (Perl, Ruby on Rails, Python with Flask) if the server raises an unhandled exception I can see the error message on the server side somewhere: in a log file, on standard output, somewhere. But in this FastAPI/uvicorn application I cannot find any error message. I don't see it in the place where I ran the wget and I don't see it in the uvicorn terminal.
Where is the 500 error message logged/displayed?
I don't think this is an issue with uvicorn. If there is an error it displays it on terminal by default unless you have changed some configuration. If you haven't changed any configuration and still not seeing the log you can pass the log configuration file during command.
filename: log_config.yml
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"verbose": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s"
}
},
"handlers": {
"uvicorn": {
"class": "logging.handlers.TimedRotatingFileHandler",
"level": "DEBUG",
"formatter": "verbose",
"when": "D",
"backupCount": 0,
"filename": "uvicorn.log"
}
},
"loggers": {
"uvicorn": {
"level": "DEBUG",
"handlers": ["uvicorn"],
"propagate": true,
"qualname": "uvicorn"
}
}
}
command: uvicorn api.main:app --reload --log-config=log_config.yml
After this you will see the logs in uvicorn.log file.
Note:You may need to create uvicorn.log file beforehand
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With