Skip to main content

flask_google_cloud_logger

CircleCI PyPI version Maintainability Test Coverage

Flask extension to format logs according to Google Cloud v2 Specification

Python log formatter for Google Cloud according to v2 specification using python-json-logger formatter

Inspired by Elixir's logger_json

Instalation

Pipenv

    pipenv install flask_google_cloud_logger 

Pip

    pip install flask_google_cloud_logger 

Usage

import logging
from logging import config

from flask import Flask, request, g
from flask_google_cloud_logger import FlaskGoogleCloudLogger

LOG_CONFIG = {
    "version": 1,
    "formatters": {
        "json": {
            "()": "flask_google_cloud_logger.FlaskGoogleCloudFormatter",
            "application_info": {
                "type": "python-application",
                "application_name": "Example Application"
            },
            "format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s"
        }
    },
    "handlers": {
        "json": {
            "class": "logging.StreamHandler",
            "formatter": "json"
        }
    },
    "loggers": {
        "root": {
            "level": "INFO",
            "handlers": ["json"]
        },
        "werkzeug": {
            "level": "WARN",  # Disable werkzeug hardcoded logger
            "handlers": ["json"]
        }
    }
}

config.dictConfig(LOG_CONFIG)  # load log config from dict
logger = logging.getLogger("root")  # get root logger instance
app = Flask("test_app")
FlaskGoogleCloudLogger(app)


@app.route("/")
def hello_world():
    return "Hello, World!"


@app.teardown_request #log request and response info after extension's callbacks
def log_request_time(_exception):
    logger.info(
        f"{request.method} {request.path} - Sent {g.response.status_code}" +
        " in {g.request_time:.5f}ms")

Example output:

{"timestamp": "2018-11-04T22:07:42.687000Z", "severity": "INFO", "message": "GET / - Sent 200 in {g.request_time:.5f}ms", "labels": {"type": "python-application", "application_name": "Example Application", "client": {"user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36", "ip": "127.0.0.1", "version": null}, "connection": {"method": "GET", "path": "/", "request_id": "afc4c6d5-5f30-4f02-b66a-e97e87841073", "status": 200}, "latency": 0.13828277587890625}, "metadata": {"userLabels": {}}, "sourceLocation": {"file": "test_app.py", "line": 52, "function": "log_request_time"}}

Credits

Thanks @thulio, @robsonpeixoto, @ramondelemos

Metadata

Release files for flask-google-cloud-logger 0.2.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-google-cloud-logger 0.2.1
File Size Uploaded Provenance Actions
flask_google_cloud_logger-0.2.1.tar.gz 4.3 kB Without provenance Details for flask_google_cloud_logger-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-google-cloud-logger 0.2.1
File Interpreter ABI Platform Actions
flask_google_cloud_logger-0.2.1-py3-none-any.whl Python 3 none any Details for flask_google_cloud_logger-0.2.1-py3-none-any.whl

Total release size: 10.2 kB

Release files / flask_google_cloud_logger-0.2.1.tar.gz

Download URL flask_google_cloud_logger-0.2.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cc97442c1efb7b5f569289bcb542a8a7aecb20fc3f9b7c31a7f7fa0845089dcb
BLAKE2b-256 checksum
How to use checksums
572a2e47d97abda2297cd0bff8f96a7aa52f79b05de7b82800abde4f5249be8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.1

Release files / flask_google_cloud_logger-0.2.1-py3-none-any.whl

Download URL flask_google_cloud_logger-0.2.1-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d017df6a2f936c099ecb9f23c7843bdd109542c5dd55095211cb2ec963e48ed
BLAKE2b-256 checksum
How to use checksums
edabab2f06d5e3cb328dacb127117f6646b3768b16110eb9a5b8cb530c828475
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.1

Security for flask-google-cloud-logger 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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