How To Override Uvicorn Logger in FastAPI using Loguru
Logging is very important for any web application, it helps us to debug faster, a good Logging implementation saves us a lot of time.
Logging is a mean of tracking the events that happen when some web application runs. Logging is important for software developing, debugging and running. If you don’t have any logging record and your program crashes, there are very little chances that you will able to detect the cause of the problem. And if you detect the cause, it will consume a lot of time. With logging, you can leave a trail of breadcrumbs so that if something goes wrong, we can determine the cause of the problem.
Python has a built-in module logging which allows writing status messages to a file or any other output streams. The file can contain the information on which part of the code is executed and what problems have been arisen.
In this post, we talk about our challenges in figuring out how to implement a logger in FastAPI.
Why we chose FastAPI?
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. (official document)
- Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
- Robust: Get production-ready code. With automatic interactive documentation.
- Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
- Fewer bugs: Reduce about 40% of human (developer) induced errors. *
- Intuitive: Great editor support. Completion everywhere. Less time debugging.
$ pip install fastapi
What is Uvicorn and what’s its role in FastAPI deployment ?
Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools. Until recently Python has lacked a minimal low-level server/application interface for asyncio frameworks. The ASGI specification fills this gap, and means we’re now able to start building a common set of tooling usable across all asyncio frameworks.
Uvicorn used as application server in Fastapi.
$ pip install uvicorn
What’s the problem?
Generally it is very difficult to override default uvicorn logs. When one deploys an application using Uvicorn, this is how the default logs look like.
From these system logs we can observe that, logs are not in detail and only few information were given. Another problem is that it is very difficult to write these logs in a log file.
We try to resolve this problem using custom logs implemented using Loguru which is a library that aims to bring enjoyable logging in Python.(official document)
$ pip install loguru
Project Structure
logging_config.json
It basically contain logging config and other application config. In this example we only store logging configuration.
For more customization follow loguru api documentation
main.py
costum_logging.py
Create a requirement.txt file
loguru==0.4.1
fastapi
uvicorn
So everything is ready!! Excited to see the power of loguru.
Installation and Setup
- Create Virtual Environment
# Install python virtual environment
$ pip install virtualenv# Check virtual environment version
$ virtualenv --version# Now Create virtual environment
$ virtualenv my_name# Activate Virtual environment
$ source virtualenv_name/bin/activate
- Install requirements
$ pip install -r requirement.txt
- Run your Application
$ uvicorn main:app --port 5008 --access-log
The Final Output
Using our custom logger implementation, we are able to achieve logging in the desired format.
Conclusion
- We can use uvicorn default logging, there is no harm in it but your web application logs must be customize as per your needs. So you can try this solution.
- Easy to use and customize with loguru. (You can change you logging config for more customization)
- Produce full details of error.
- Easy to store in a specific log file. (which you mention in logging config)
- Better traceback.