Configure LDP logging handlers and middleware for your Flask app.
Project description
Flask-LDP
Work in progres ...
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Flask-LDP-0.1.1.tar.gz
.
File metadata
- Download URL: Flask-LDP-0.1.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7280f2e72a265d005b160aa6e4830c3905dad6c64838eca6fe83bfb7e9b19f4 |
|
MD5 | 4a04673ff3da9cca10f12f44a28ead4d |
|
BLAKE2b-256 | ccf3fb28d527dbeb1db998604f64c45963927bb00b92824b9ffcfd2a1fca48a0 |
File details
Details for the file Flask_LDP-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: Flask_LDP-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30c8a7d5a4b7d0c523a6719f13685b8d290c545945736dfd4182f8c7ab154024 |
|
MD5 | 47416f810e3833b1960d1dcce1514ab0 |
|
BLAKE2b-256 | 56ee664d2277361ee11449a4c882815f71076a667ed911235d059bac328f7838 |