Custom Cloud Logging handler for Flask applications deployed in Google App Engine. Groups logs coming from the same request lifecycle and propagates the maximum log level throughout the request lifecycle using middleware and context management.
Project description
flask-gae-logging
Custom Cloud Logging handler for Flask applications deployed in Google App Engine to ease out logs analysis and monitoring through Google Cloud Log Explorer.
What problem does this package solve? Why do we need this?
-
Log Severity Mismatch: When deploying Flask applications on Google App Engine with Python3 runtime and using
google-cloud-loggingthe logs of each request lifecycle can be viewed into a group of the request lifecycle. The groupped logs functionality is natively supported ingoogle-cloud-loggingand one can view logs by request lifecycle using therequest_loglogger in the Cloud Log Explorer. However, the severity level of logs is not properly propagated throughout the request lifecycle to the parent log in groupped logs. This means that if a warning or error occurs at any point in the request, the parent log will not reflect the severity on the final outcome, making it harder to identify problematic requests. -
Payload Logging Issues: Capturing and logging request payloads was cumbersome, requiring extra logging in the handlers and extra deployments. This led to incomplete logs, making it harder to reproduce issues or analyze request content.
-
Inconsistent Log Structures: The default logging setup lacked a consistent structure, which made it challenging to filter, search, and analyze logs in the Google Cloud Log Explorer.
So what does it do?
The flask-gae-logging module addresses these problems by:
-
Log Level Propagation: The maximum log level observed during a request's lifecycle is propagated, ensuring that logs associated with a failed request reflect the appropriate severity. This improves the accuracy and utility of log searches based on severity.
-
Structured Payload Logging: Request payloads are captured and logged in a structured format, even for non-dictionary JSON payloads. This ensures that all relevant request data is available for analysis, improving the ability to diagnose issues.
Install
pip install flask-gae-logging
Features:
- Request Maximum Log Level Propagation: Propagates the maximum log level throughout the request lifecycle, making it easier to search logs based on the severity of an issue.
- Optional incoming request logging: Opt in/out to log headers and payload of incoming requests into the
jsonPayloadfield of the parent log. - Optional request headers logging: Defaults to True. Headers dict lands into field
request_headersin thejsonPayloadof parent log. - Request Payload Logging: Defaults to True. Incoming payload parsed lands into field
request_payloadin thejsonPayloadof parent log. Parsing is based on content type with capability to override. Currenty embedded parsers for:application/jsonapplication/x-www-form-urlencodedmultipart/form-datatext/plain
- Optional add-on log filters:
GaeLogSizeLimitFilterfilter to drop log records if they exceed the maximum allowed size by google cloud logging.GaeUrlib3FullPoolFilterfilter to drop noisy 'Connection pool is full' warning logs from Google Cloud and App Engine internal libraries.
API
- Initialization
FlaskGAEMaxLogLevelPropagateHandler(
app: Flask,
request_logger_name: Optional[str] = None,
log_payload: bool = False,
log_headers: bool = False,
builtin_payload_parsers: Optional[List["PayloadParser.Defaults"]] = None,
custom_payload_parsers: Optional[Dict[str, Callable[[], object]]] = None,
*args, **kwargs
)
- Parameters
-
app (Flask): The Flask application instance.
-
request_logger_name (Optional[str], optional): The name of the Cloud Logging logger to use for request logs. Defaults to the Google Cloud Project ID with the suffix '-request-logger'.
-
log_payload (bool, optional): Whether to log the request payload. If True, the payload for POST, PUT, PATCH, and DELETE requests will be logged. Defaults to False.
-
log_headers (bool, optional): Whether to log the request headers. Defaults to False.
-
builtin_payload_parsers (List["PayloadParser.Defaults"], optional): A list of built-in parser functions for logging request payloads. Defaults to None.
-
custom_payload_parsers (Dict[str, Callable], optional): A dictionary mapping content types to custom parser functions for logging request payloads. If provided, these will override default parsers. Defaults to None.
-
*args: Additional arguments to pass to the superclass constructor. Any argument you would pass to CloudLoggingHandler.
-
**kwargs: Additional keyword arguments to pass to the superclass constructor. Any keyword argument you would pass to CloudLoggingHandler.
-
Example of usage
import logging
import os
from flask import Flask, jsonify, request
app = Flask(__name__)
def custom_payload_parser_plain_text():
# Custom parser for text/plain to demonstrate GAE handler extensibility.
# Needs to return a serializable value to be logged
incoming_payload = request.data.decode('utf-8', errors='replace')
return f"Parsed Plain Text: {incoming_payload}"
# Initialize GAE Logging
if os.getenv('GAE_ENV', '').startswith('standard'):
import google.cloud.logging
from google.cloud.logging_v2.handlers import setup_logging
from flask_gae_logging import (
FlaskGAEMaxLogLevelPropagateHandler,
GaeLogSizeLimitFilter,
GaeUrlib3FullPoolFilter,
PayloadParser,
)
client = google.cloud.logging.Client()
gae_log_handler = FlaskGAEMaxLogLevelPropagateHandler(
app=app,
client=client,
# Optional - opt in for logging payload and logs; defaults are False
log_headers=True,
log_payload=True,
# Optional - opt in for all built in payload parsers; applicable only if log_payload is set True
builtin_payload_parsers=[content_type for content_type in PayloadParser.Defaults],
# Optional - override built in payload parsers or provide more; applicable only if log_payload is set True
custom_payload_parsers={
"text/plain": custom_payload_parser_plain_text
}
)
setup_logging(handler=gae_log_handler)
# Optional - add extra filters for the logger
gae_log_handler.addFilter(GaeLogSizeLimitFilter())
gae_log_handler.addFilter(GaeUrlib3FullPoolFilter())
logging.getLogger().setLevel(logging.DEBUG)
@app.errorhandler(Exception)
def handle_exception(e):
logging.exception("Uncaught exception occurred")
return jsonify({"error": str(e)}), 500
@app.route('/info', methods=['GET'])
def info():
logging.debug("Step 1: Debugging diagnostic")
logging.info("Step 2: General information log")
return jsonify({"message": "info"})
@app.route('/warning', methods=['GET'])
def warning():
logging.debug("Step 1: Check system state")
logging.info("Step 2: State is normal")
logging.warning("Step 3: Resource usage approaching threshold")
return jsonify({"message": "warning"})
@app.route('/error', methods=['GET'])
def error():
logging.debug("Step 1: Internal check")
logging.info("Step 2: Transaction started")
logging.warning("Step 3: Retry attempted")
logging.error("Step 4: Transaction failed after retries")
return jsonify({"message": "error"})
@app.route('/exception', methods=['GET'])
def exception():
logging.debug("Step 1: Preparing logic")
logging.info("Step 2: Executing risky operation")
logging.error("Step 3: Critical failure detected")
raise ValueError("Simulated ValueError for GAE grouping demonstration")
@app.route('/http_exception', methods=['GET'])
def http_exception():
logging.debug("Step 1: Looking up resource")
logging.info("Step 2: Resource ID not found in database")
return jsonify({"error": "Resource not found"}), 404
@app.route('/post_payload', methods=['POST'])
def post_payload():
content_type = request.headers.get('Content-Type', '')
logging.debug(f"Handling POST request with Content-Type: {content_type}")
# 1. Handle JSON
if request.is_json:
payload = request.get_json(silent=True)
logging.info(f"Parsed as JSON: {payload}")
# 2. Handle Form URL-Encoded
elif content_type == "application/x-www-form-urlencoded":
# Flask populates request.form for this content type
payload = request.form.to_dict()
logging.info(f"Parsed as Form URL-Encoded: {payload}")
# 3. Fallback for Plain Text or others
else:
payload = request.data.decode('utf-8', errors='replace')
logging.info(f"Parsed as Raw/Text: {payload}")
return jsonify({
"mirror_response": payload,
"detected_type": str(type(payload)),
"content_type_received": content_type
}), 200
@app.route("/post_form", methods=["POST"])
def post_form():
description = request.form.get("description")
file = request.files.get("file")
if not description or not file:
logging.warning("Incomplete form submission received")
return jsonify({"error": "Missing required fields"}), 400
file_content = file.read()
payload = {
"description": description,
"file_name": file.filename,
"content_type": file.content_type,
"file_size": len(file_content),
}
logging.info(f"Form submission processed: {payload}")
return jsonify({"mirror_response": payload}), 200
How it looks in Google Cloud Log Explorer
Logger selection
Groupped logs with propagated log severity to the parent log
Grouped logs in request with payload
Dependencies
This tool is built upon the following packages:
flask: A lightweight WSGI web application framework.google-cloud-logging: Google Cloud Logging API client library for logging and managing logs in Google Cloud Platform.
Dev
uv sync --all-packages- Use
sample_appfolder for minimal Appengine app deployment of flask app that uses the local librarysrccode via symlink.- If symlink is broken for any reason, create it again from inside the
sample_appfolder:ln -s ../src/flask_gae_logging/ . - Deploy the app:
gcloud app deploy --version=v1 default.yaml --project=<PROJECT_ID> --account <ACCOUNT_EMAIL> - Ping the sample app to generate logs for various cases in log explorer:
python3.12 ping_endpoints.py --project <PROJECT_ID>
- If symlink is broken for any reason, create it again from inside the
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_gae_logging-0.1.1.tar.gz.
File metadata
- Download URL: flask_gae_logging-0.1.1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d52140fc030b4f6d0c2ae517e10be9f4885c951cb7b2ccc16aba47a56e4335e5
|
|
| MD5 |
211ab38557b5ebb6426ea6aba2b8f788
|
|
| BLAKE2b-256 |
eb17a0ae575eaa95e1dfd87cd2a48fa5c3d7390b1cb65e96953b11fb96977667
|
Provenance
The following attestation bundles were made for flask_gae_logging-0.1.1.tar.gz:
Publisher:
ci_cd.yml on trebbble/flask-gae-logging
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_gae_logging-0.1.1.tar.gz -
Subject digest:
d52140fc030b4f6d0c2ae517e10be9f4885c951cb7b2ccc16aba47a56e4335e5 - Sigstore transparency entry: 804970634
- Sigstore integration time:
-
Permalink:
trebbble/flask-gae-logging@0904ed615d11f0c750bad9d5e2503fde666c67b8 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/trebbble
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@0904ed615d11f0c750bad9d5e2503fde666c67b8 -
Trigger Event:
release
-
Statement type:
File details
Details for the file flask_gae_logging-0.1.1-py3-none-any.whl.
File metadata
- Download URL: flask_gae_logging-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b994a51a631ba835feaf560b3dc15ea5f650c8af62706339661599d54bcf139b
|
|
| MD5 |
698d117ce86f409f46380b84efaf7a4b
|
|
| BLAKE2b-256 |
85476e0b1b57e17ff0b9f298cef9a5e2b573be1dba502e80f76b0a0ab585e3b9
|
Provenance
The following attestation bundles were made for flask_gae_logging-0.1.1-py3-none-any.whl:
Publisher:
ci_cd.yml on trebbble/flask-gae-logging
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_gae_logging-0.1.1-py3-none-any.whl -
Subject digest:
b994a51a631ba835feaf560b3dc15ea5f650c8af62706339661599d54bcf139b - Sigstore transparency entry: 804970645
- Sigstore integration time:
-
Permalink:
trebbble/flask-gae-logging@0904ed615d11f0c750bad9d5e2503fde666c67b8 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/trebbble
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@0904ed615d11f0c750bad9d5e2503fde666c67b8 -
Trigger Event:
release
-
Statement type: