Skip to main content

Python Flask Middleware to log and set Request ID in the HTTP header

Project description

Flask Header Log Request ID

A Flask middleware to log and set Request ID in the HTTP header.

Overview

This project provides a Flask middleware that ensures a unique Request ID is generated and logged for each incoming request. The Request ID is also included in the response back to the client.

Features

Generates a unique Request ID for each incoming request Logs the Request ID using a log filter Includes the Request ID in the response back to the client Installation To install the middleware, run the following command:

pip install flask_header_log_request_id

Usage

To use the middleware in your Flask application, simply import and initialize it:

from flask import Flask
from flask_header_log_request_id import RequestID

app = Flask(__name__)
RequestID(app)

Configuration

The middleware can be configured using the following settings:

  • REQUEST_ID_UNIQUE_VALUE_PREFIX: a prefix that indicates a request ID should be considered unique

Logging

The middleware uses a log filter to inject the current request ID into log records. To use the log filter, add it to your logging configuration:

from flask_header_log_request_id.log_filter import RequestIDLogFilter

logging.basicConfig()
logger = logging.getLogger()
logger.addFilter(RequestIDLogFilter())

Example

Here is an example of how to use the middleware in a Flask application:

from flask import Flask
from flask_header_log_request_id import RequestID

app = Flask(__name__)
RequestID(app)

@app.route("/")
def index():
    return "Hello, World!"

if __name__ == "__main__":
    app.run()

In this example, the middleware will generate a unique Request ID for each incoming request and log it using the log filter. The Request ID will also be included in the response back to the client.

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

flask_header_log_request_id-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_header_log_request_id-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_header_log_request_id-0.1.0.tar.gz.

File metadata

File hashes

Hashes for flask_header_log_request_id-0.1.0.tar.gz
Algorithm Hash digest
SHA256 04325902c11445191a8f022bb229445ab6dc05bbac4144fc7f648435d3d7988b
MD5 696832995214efcc65e078cd02b119c8
BLAKE2b-256 06b5f2be7e89d812974d7cc41ff017858989aaa4dab4ee0d77aa90fd26066fca

See more details on using hashes here.

File details

Details for the file flask_header_log_request_id-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_header_log_request_id-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4860b2429e6abb9332031c85eb9d7f880247db9e964024039937a6631117d702
MD5 b5a032f094a044c2c17b59f06bfb9fca
BLAKE2b-256 d373fc96a720f7e55db7da526c2502963b9b78b0f3baaa9654a18c44e072e2df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page