Skip to main content

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

https://raw.githubusercontent.com/salrashid123/flask-gcp-log-groups/master/images/log_entry.png

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= {
              "type": "gce_instance",
              "labels": { "instance_id": "5160310737730769780",
                          "zone": "us-central1-a"
                        }
    })
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-gcp-log-groups-0.2.2.tar.gz (5.9 kB view hashes)

Uploaded Source

Supported by

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