Skip to main content

Custom Cloud Logging handler for FastAPI 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

fastapi-gae-logging

Custom Cloud Logging handler for FastAPI 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.

Install

pip install fastapi-gae-logging

Features:

  • Request Logs Grouping: Groups logs coming from the same request lifecycle to ease out log analysis using the Google Cloud Log Explorer. Grouping logger name can be customised and it defaults to the Google Cloud Project ID with '-request-logger' as a suffix.
  • Request Maximum Log Level propagation: Propagates the maximum log level throughout the request lifecycle to ease out log searching based on severity of an issue.
  • Request Payload Logging - More options to be exposed on that behaviour:
    • In case the request contains a payload, then this is logged into the parent log of request grouped logs
    • For now content type of payload is assumed to be a valid JSON, otherwise it is discarded. More to be supported later on.
    • The payload is logged as a dictionary using the google-cloud-logging's method for log_struct. Since that method is logging only dictionaries, any JSON valid payload that is not a dictionary is wrapped into a dummy keyword to constuct one. The dummy keyword looks like that: <original_type_of_payload>__payload_wrapper The original or virtual dictionary is logged with log_struct along with the parent log of request grouped logs and ends up in field jsonPayload.

API

  • Custom Cloud Logging Handler to use with official library google-cloud-logging: FastAPIGAELoggingHandler

Example

from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
import logging
import os


app = FastAPI()

if os.getenv('GAE_ENV', '').startswith('standard'):
    import google.cloud.logging
    from google.cloud.logging_v2.handlers import setup_logging
    from fastapi_gae_logging import FastAPIGAELoggingHandler

    client = google.cloud.logging.Client()
    gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client)
    setup_logging(handler=gae_log_handler)


logging.getLogger().setLevel(logging.DEBUG)


@app.get("/info")
def info():
    logging.debug("this is a debug")
    logging.info("this is an info")
    return JSONResponse(
        content={
            "message": "info"
        }
    )


@app.get("/warning")
async def warning():
    logging.debug("this is a debug")
    logging.info("this is an info")
    logging.warning("this is a warning")
    return JSONResponse(
        content={
            "message": "warning"
        }
    )


@app.get("/error")
def error():
    logging.debug("this is a debug")
    logging.info("this is an info")
    logging.warning("this is a warning")
    logging.error("this is an error")
    return JSONResponse(
        content={
            "message": "error"
        }
    )


@app.get("/exception")
def exception():
    logging.debug("this is a debug")
    logging.info("this is an info")
    logging.error("this is an error")
    raise ValueError("This is a value error")


@app.get("/http_exception")
def http_exception():
    logging.debug("this is an debug")
    logging.info("this is an info")
    raise HTTPException(
        status_code=404,
        detail={
            "error": "Resource not found"
        }
    )

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

fastapi_gae_logging-0.0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

fastapi_gae_logging-0.0.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_gae_logging-0.0.2.tar.gz.

File metadata

  • Download URL: fastapi_gae_logging-0.0.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for fastapi_gae_logging-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5fd76bc838b60fa95a7411ecce4583e646ade6df8b057104d5f8103b1d306f1b
MD5 11a980db3827550b640e57d7ae339799
BLAKE2b-256 5b59d45824caa54ad870619f7bb6fac14f5ab3baffb4980a04fe73877f74a373

See more details on using hashes here.

File details

Details for the file fastapi_gae_logging-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_gae_logging-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 31380c1346f505109f394e424f872de25bb93ab2b7c715409a3a6ec20dcda1ce
MD5 a023187d72c6a3d596d1926bae39c5b6
BLAKE2b-256 db2a5528a8e3f2aee14b3b0f0d222998514f59ea9bd8d2dad73297d674f751ed

See more details on using hashes here.

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