Skip to main content

Flask-LDP

Work in progres ...

PyPI version

Fork of https://github.com/gridscale/flask-graylog2 to support OVH Logs Data Platform.

Which is himself a fork of github.com/underdogio/flask-graylog with additional patches and features.

This is a Flask extension that allows you to configure a OVH Logs Data Platform (LDP) logging handler as well as some middleware to log every request/response pair to Graylog.

See also:

Installation

You can install it with pip:

$ pip install Flask-LDP

Usage

You only need to import and initialize your app

# Import dependencies
from flask import Flask
from flask_ldp import LDP

# Configure app and LDP logger
app = Flask(__name__)
ldp = LDP(app)

# Log to ldp
ldp.info("Message", extra={"data": "metadata",})

# Use LDP log handler in another logger
import logging

logger = logging.getLogger(__name__)
logger.addHandler(ldp.handler)
logger.info("Message")

⚙️ Configuration options

The following options can be use to configure the ldp logger.

from flask import Flask
from flask_ldp import LDP

app = Flask(__name__)

# Use configuration from `app`
app.config["LDP_HOSTNAME"] = "gra3.logs.ovh.com"
app.config["LDP_TOKEN"] = "xxxxxx"
ldp = LDP(app)

# Provide configuration
config = {"LDP_HOSTNAME": "gra3.logs.ovh.com", "LDP_TOKEN": "xxxxx"}
ldp = LDP(app, config=config)
  • LDP_HOSTNAME - the host to send messages to [default: 'gra3.logs.ovh.com']
  • LDP_TOKEN - the token [default: None]

🪵 Additionnal data to log

You can send a few extra data, as provided with by the default schema.

class DefaultLoggingkSchema(LDPSchema):
    user = fields.Raw()
    data = fields.Raw()
    flask = fields.Raw()
    request = fields.Raw()
    response = fields.Raw()

Example

something_json_dumpable = dict(toto='titi')
extra_data = dict(
    data=something_json_dumpable,
    user=something_json_dumpable,
)
ldp.info("Message", extra=extra_data)

You can also dump flask information and request data

ldp.info("Message", add_flask=True, add_request=True)

# Under the hood
if 'add_flask' in kwargs:
    extra['flask'] = {"endpoint": str(request.endpoint).lower(), "view_args": request.view_args}
    del kwargs['add_flask']

if 'add_request' in kwargs:
    extra['request'] = {
        "content_length": request.environ.get("CONTENT_LENGTH"),
        "content_type": request.environ.get("CONTENT_TYPE"),
        "method": request.environ.get("REQUEST_METHOD"),
        "path_info": request.environ.get("PATH_INFO"),
        "query_string": request.environ.get("QUERY_STRING"),
        "remote_addr": request.environ.get("REMOTE_ADDR"),
        "headers": dict(
            (key[5:].replace("-", "_").lower(), value)
            for key, value in request.environ.items()
            if key.startswith("HTTP_") and key.lower() not in ("http_cookie",)
        ),
    }
    del kwargs['add_request']

💡 Ideas for improvements

  • Make the schema configurable with config
  • Add middleware so as to log response as well (like the graylog extension)
  • Add decorator so as to enable log for a specific endpoint (with user request and response automatically added)

Metadata

Release files for Flask-LDP 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-LDP 0.1.1
File Size Uploaded
Flask-LDP-0.1.1.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-LDP 0.1.1
File Interpreter ABI Platform
Flask_LDP-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.5 kB

Release files / Flask-LDP-0.1.1.tar.gz

Download URL Flask-LDP-0.1.1.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c7280f2e72a265d005b160aa6e4830c3905dad6c64838eca6fe83bfb7e9b19f4
BLAKE2b-256 checksum
How to use checksums
ccf3fb28d527dbeb1db998604f64c45963927bb00b92824b9ffcfd2a1fca48a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

Release files / Flask_LDP-0.1.1-py3-none-any.whl

Download URL Flask_LDP-0.1.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30c8a7d5a4b7d0c523a6719f13685b8d290c545945736dfd4182f8c7ab154024
BLAKE2b-256 checksum
How to use checksums
56ee664d2277361ee11449a4c882815f71076a667ed911235d059bac328f7838
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page