Releases: chrisK824/fastapi-gae-logging
Releases · chrisK824/fastapi-gae-logging
v0.0.10
v0.0.9
Breaking changes
- Request payload is now logged under a new nested field
request_payload
in thejsonPayload
field of each request lifecycle parent request. Check screenshot in readme to locate the new structure in parent log when looking into Google Cloud Logging Explorer.
New features
- Added optional logging of request headers into the parent log. This is optional but defaults to true. The headers are placed into
request_headers
nested field in thejsonPayload
field of parent log. - Request payload is parsed automatically before logged in
request_payload
nested field. Parsing happens based on content type of incoming request and currently there are 3 types supported:application/json
application/x-www-form-urlencoded
text/plain
- User can add more parsers or override existing ones by passing extra parsers as a dict during logging initialization. Example:
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
import logging
import os
app = FastAPI()
async def custom_payload_parser_plain_text(request: Request):
try:
body_bytes = await request.body()
incoming_payload = body_bytes.decode('utf-8')
return f"This was the original request payload: {incoming_payload}"
except Exception as e:
return f"Failed to read request payload as plain text: {e} | {traceback.format_exc()}"
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()
# overriding default parsing for payload when content type is 'text/plain'
gae_log_handler = FastAPIGAELoggingHandler(
app=app,
client=client,
custom_payload_parsers={
"text/plain": custom_payload_parser_plain_text
}
)
# use the log_payload parameter if you want to opt-out from payload logging
# gae_log_handler = FastAPIGAELoggingHandler(app=app, client=client, log_payload=False)
setup_logging(handler=gae_log_handler)
v0.0.8
Dropped usage and dependency directly on FastAPI and replaced with Starlette instead. This way this package can be used with pure Starlette applications as well and even maybe with any other ASGI Starlette based frameworks that implement or inherit Starlette's add_middleware
method for attaching pure ASGI middlewares.
Kudos to @iffius for pointing that out!
v0.0.7
v0.0.6
v0.0.5
v0.0.4
v0.0.3
V0.0.2
Initial release
0.0.1 add first version