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 details)

Uploaded Source

File details

Details for the file flask-gcp-log-groups-0.2.2.tar.gz.

File metadata

  • Download URL: flask-gcp-log-groups-0.2.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for flask-gcp-log-groups-0.2.2.tar.gz
Algorithm Hash digest
SHA256 8f7037e0b01e8dec43c2fe367c6a3c239f41daf3685d7267bfd40c1b7fb69e24
MD5 52ca6325594cbaa1f63d5a128203a6e3
BLAKE2b-256 0f45b5370fb476e2786492754a2819f0feb48c87a3f2d4efda5eb4f193a2e99e

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