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.
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
Built Distribution
File details
Details for the file fastapi_gae_logging-0.0.1.tar.gz
.
File metadata
- Download URL: fastapi_gae_logging-0.0.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7f85b7dd79ce922c80fe519fe393393a862d2677ee7d09223ba078cd9981a57 |
|
MD5 | 8a13878c212adf88a77a406329a719e2 |
|
BLAKE2b-256 | 24d05a5934a3dbba1ab1adcc9e829c02f981af62f86ac807d5cd41e086f774a2 |
File details
Details for the file fastapi_gae_logging-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: fastapi_gae_logging-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d7110634d6817742bf586c5325ffc39f618a6f7a2e1b01d85b153f272e51369 |
|
MD5 | a0554630fc806256aa8ca8e7490150e3 |
|
BLAKE2b-256 | c7f4b20fac546cf1f3288b95629271ff0295c9f9b0d065b2b222c22bb3772dcc |