Google Cloud Log Formatter for Flask
Project description
flask_google_cloud_logger
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
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
Built Distribution
Close
Hashes for flask_google_cloud_logger-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f251c875f759bc38150da0536cd7882feef354d6a53acb2c942cdbd4150d5514 |
|
MD5 | 7c55df643bd371a32816208a711be172 |
|
BLAKE2b-256 | eae176b2351094ec2dc2ab146a6c715d73f24a8ebe5fa237f42e80a37077b5dc |
Close
Hashes for flask_google_cloud_logger-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a4ef774d386408f71638b371fc646dc023fba937302d5d8d98553abfa8d4a6e |
|
MD5 | 7daa72bfb8b611d59eb7fcacd0d83028 |
|
BLAKE2b-256 | 44b31881a62f0ce2840bf29e5ecd899a825c0452e352760b246a4efff5bf8c44 |