How To Change Log Base On Ti-83 Plus
How To Override Uvicorn Logger in FastAPI using Loguru
Logging is very important for whatsoever web awarding, information technology helps us to debug faster, a skillful Logging implementation saves united states of america a lot of time.
Logging is a hateful of tracking the events that happen when some spider web application runs. Logging is of import for software developing, debugging and running. If you lot don't accept any logging record and your program crashes, there are very niggling chances that y'all will able to detect the crusade of the trouble. And if you detect the cause, it will eat a lot of fourth dimension. With logging, you can exit a trail of breadcrumbs then that if something goes wrong, we can determine the cause of the problem.
Python has a built-in module logging which allows writing condition messages to a file or any other output streams. The file can contain the information on which office of the code is executed and what bug have been arisen.
In this post, we talk about our challenges in figuring out how to implement a logger in FastAPI.
Why nosotros chose FastAPI?
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python three.6+ based on standard Python blazon hints. (official document)
- Very loftier operation, on par with NodeJS and Go (thanks to Starlette and Pydantic). Ane of the fastest Python frameworks bachelor.
- Robust: Get production-prepare code. With automatic interactive documentation.
- Standards-based: Based on (and fully compatible with) the open up 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 fourth dimension debugging.
$ pip install fastapi
What is Uvicorn and what'south 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 ways nosotros're at present able to start edifice a common prepare of tooling usable across all asyncio frameworks.
Uvicorn used as application server in Fastapi.
$ pip install uvicorn
What'due south the trouble?
Generally it is very hard to override default uvicorn logs. When ane deploys an application using Uvicorn, this is how the default logs expect like.
From these system logs we tin observe that, logs are not in detail and merely few information were given. Another problem is that it is very difficult to write these logs in a log file.
We endeavor 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
Projection Structure
logging_config.json
It basically contain logging config and other application config. In this example we merely store logging configuration.
For more customization follow loguru api documentation
master.py
costum_logging.py
Create a requirement.txt file
loguru==0.4.1
fastapi
uvicorn
So everything is set up!! Excited to see the power of loguru.
Installation and Setup
- Create Virtual Environment
# Install python virtual surround
$ pip install virtualenv # Check virtual environment version
$ virtualenv --version # Now Create virtual environment
$ virtualenv my_name # Activate Virtual environs
$ source virtualenv_name/bin/activate
- Install requirements
$ pip install -r requirement.txt
- Run your Application
$ uvicorn master:app --port 5008 --admission-log
The Final Output
Using our custom logger implementation, we are able to attain logging in the desired format.
Decision
- We tin can use uvicorn default logging, in that location is no damage in it but your spider web awarding logs must be customize as per your needs. So you tin try this solution.
- Easy to use and customize with loguru. (You lot can change you logging config for more customization)
- Produce total details of fault.
- Piece of cake to store in a specific log file. (which you mention in logging config)
- Better traceback.
If you institute this helpful, please clap and share the article. Permit's spread the solution to someone who may need it.
Source: https://medium.com/1mgofficial/how-to-override-uvicorn-logger-in-fastapi-using-loguru-124133cdcd4e
Posted by: henrypeargen.blogspot.com
0 Response to "How To Change Log Base On Ti-83 Plus"
Post a Comment