Simple library to customize logger for FastAPI
Project description
FastAPI MiddleWare Logger
This library is a very simple tool to simplify custom formatting of logs for FastAPI. It's almost entirely based on this answer from SO.
Usage
The design of this library is made to be very simple to use. Instead of instantiating FastAPI
, just instatiate FastAPIMiddleWareLogger
:
from fastapi_json_logger import FastAPIMiddleWareLogger
app = FastAPIMiddleWareLogger()
@app.get("/")
def get_index():
return {"status": "ok"}
You can change the logger by providing a custom_logger
function:
from fastapi_json_logger import FastAPIMiddleWareLogger
def my_custom_logger(response_status_code, **kwargs):
if response_status_code != 200:
logging.error("Some error happened")
app = FastAPIMiddleWareLogger(custom_logger=my_custom_logger)
@app.get("/")
def get_index():
return {"status": "ok"}
The custom_logger
can take the following arguments:
request_body
: body of the request as a stringrequest_headers
: headers of the request as a dictionaryrequest_query_params
: query parameters of the request as a dictionaryrequest_method
: method of the request as a stringrequest_url
: URL of the request as a stringresponse_body
: body of the response as a stringresponse_headers
: headers of the response as a dictionaryresponse_media_type
: media type of the response as a stringresponse_status_code
: status code of the response as an integer
By default, the custom_logger
will print out all of these arguments.
Usual uvicorn/fastapi
logs can be kept by using disable_uvicorn_logging=False
.
This could be used to perform other operations but I have not, nor will, tested it.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for fastapi-middleware-logger-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c80cf04fa8a655a039f380f03a460f392e3f532ae4018f3379205a8d8254f26d |
|
MD5 | 309d440dfad8a7baf7cc39f161f7185f |
|
BLAKE2b-256 | 9b106bf987500f9548611436de6ab4c96c95f51417ff924c7c36c481c0e418d5 |