Flask-Log-Request-Id is an extension for Flask that can parse and handle the request-id sent by request processors like Amazon ELB, Heroku Request-ID or any multi-tier infrastructure as the one used at microservices. A common usage scenario is to inject the request_id in the logging system so that all log records, even those emitted by third party libraries, have attached the request_id that initiated their call. This can greatly improve tracing and debugging of problems.
Features
Flask-Log-Request-Id provides the current_request_id() function which can be used at any time to get the request id of the initiated execution chain. It also comes with log filter to inject this information on log events as also an extension to forward the current request id into Celery’s workers.
Example: Parse request id and send it to to logging
In the following example, we will use the RequestIDLogFilter to inject the request id on all log events, and a custom formatter to print this information. If all these sounds unfamiliar please take a look at python logging system
import logging
import logging.config
from random import randint
from flask import Flask
from flask_log_request_id import RequestID, RequestIDLogFilter
def generic_add(a, b):
"""Simple function to add two numbers that is not aware of the request id"""
logging.debug('Called generic_add({}, {})'.format(a, b))
return a + b
app = Flask(__name__)
RequestID(app)
# Setup logging
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - level=%(levelname)s - request_id=%(request_id)s - %(message)s"))
handler.addFilter(RequestIDLogFilter()) # << Add request id contextual filter
logging.getLogger().addHandler(handler)
@app.route('/')
def index():
a, b = randint(1, 15), randint(1, 15)
logging.info('Adding two random numbers {} {}'.format(a, b))
return str(generic_add(a, b))
Installation
The easiest way to install it is using pip from PyPI
pip install flask-log-request-id
License
See the LICENSE file for license rights and limitations (MIT).
Metadata
Release files for Flask-Log-Request-ID 0.10.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Flask-Log-Request-ID-0.10.1.tar.gz | 7.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_Log_Request_ID-0.10.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / Flask-Log-Request-ID-0.10.1.tar.gz
| Download URL | Flask-Log-Request-ID-0.10.1.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d537a1af3776308e69435ea609230f4fb7ef162fbc4bf268d7089f0b0e1851f4
|
|
BLAKE2b-256 checksum How to use checksums |
5a59e7299f5fa5d7b5c374065d59b487c75f25df6b8c5c81e78a34f18c4acd8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9
|
Release files / Flask_Log_Request_ID-0.10.1-py3-none-any.whl
| Download URL | Flask_Log_Request_ID-0.10.1-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
04b2ab0d7eca13a816155d16dc5e5dee6a0b285c62ec2c2330394c946ff418ce
|
|
BLAKE2b-256 checksum How to use checksums |
7170c241aaa00cb35a93231bee40d39991f512ae9d5ecdc682f771441b6bcb2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9
|