Python Flask logging handler to group messages on Google Cloud Platform
Project description
Flask extension that allows log lines emitted within a request handler to display/render together.
Normally, when using Google Cloud Logging libraries ( google-cloud-logging and CloudLoggingHander), each log entry that gets emitted is displayed separately within the Logging UI. However, its desireable to group all logs together that logically belong that way in an HTTP Request. For a given HTTP Request into FLask, this extension displays all the logs ‘together’ below the parent request.
as in
Configuration Parameters
GCPHandler
app: Flask handler
parentLogName: parentLogger name for the ‘request (default: request”)
childLogName: childLogger name for the ‘application logs (default: “application”)
traceHeaderName: header name to parse as the trace header. (on GCP, its X-Cloud-Trace-Context)
labels: labels dictionary to apply to all logs (default = None),
resource: Cloud Logging resource to log against (default=’global’)
from flask import Flask
import logging, json
from flask_gcp_log_groups import GCPHandler
app = Flask(__name__)
g = GCPHandler(app, parentLogName="request",
childLogName="application",
traceHeaderName='X-Cloud-Trace-Context',
labels= {'foo': 'bar', 'baz': 'qux'},
resource='global')
g.setLevel(logging.INFO)
app.logger.addHandler(g)
@app.route('/')
def default():
app.logger.setLevel(logging.INFO)
app.logger.info("I met a traveller from an antique land,")
return 'ok'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=False)
with trace only
curl -v -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" \ -H "X-Cloud-Trace-Context: `python -c "import uuid; print uuid.uuid4()"`" \ http://localhost:8080/
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
Hashes for flask-gcp-log-groups-0.0.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 838042768f60ed90f05fd4a7f0984e268ee371a8c8b2e088a8d8f8e31af03297 |
|
MD5 | 56e8a1f23fc1b25a3f9d5dab167c4743 |
|
BLAKE2b-256 | 7c170f2e1331a1f9403145193d3aa4e954edda2958d98cd0b054ebe9efc16edb |