Skip to content

Commit

Permalink
added option to opt out for logging payload; updated readme with abso…
Browse files Browse the repository at this point in the history
…lute image addresses to fix render in pypi
  • Loading branch information
chrisK824 committed Aug 30, 2024
1 parent 0a70f98 commit 4a4babd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ def post_payload(payload: Any = Body(None)):
## How it looks in Google Cloud Log Explorer

### Logger selection
![alt text](<logger_selection.jpg>)
![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/logger_selection.jpg)

### Groupped logs with propagated log severity to the parent log

![alt text](<groupped_logs.jpg>)
![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/groupped_logs.jpg)

### Grouped logs in request with payload
![alt text](<request_with_payload.jpg>)
![alt text](https://github.com/chrisK824/fastapi-gae-logging/raw/main/request_with_payload.jpg)

## Dependencies
This tool is built upon the following packages:
Expand All @@ -139,8 +139,3 @@ This tool is built upon the following packages:
- **Cloud Logging**: Utilizes Google Cloud Logging to group logs by request and propagate the maximum log level, enhancing observability and troubleshooting.
- **Structured Logging**: Parent log of the request-response lifecycle is structured and sent to Google Cloud Logging with additional context, such as the request method, URL, and user agent after the request has been processed and served.


## Roadmap
- Allow for opting out of the dictionary enforcement of logged payload.
- Allow for opting out of request payload logging at all.
- Explore other fields in Google Cloud Logs. Investigate and consider utilizing additional fields available in Google Cloud Logs that may allow for more goodies.
24 changes: 15 additions & 9 deletions fastapi_gae_logging/fastapi_gae_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class GAERequestLogger:
logging.CRITICAL: 'CRITICAL',
}

def __init__(self, logger: Logger, resource: Resource) -> None:
def __init__(self, logger: Logger, resource: Resource, log_payload: bool = True) -> None:
"""
Initialize the GAERequestLogger.
Expand All @@ -75,6 +75,7 @@ def __init__(self, logger: Logger, resource: Resource) -> None:
"""
self.logger = logger
self.resource = resource
self.log_payload = log_payload

def _log_level_to_severity(self, log_level: int) -> str:
"""
Expand Down Expand Up @@ -117,16 +118,19 @@ async def emit_request_log(self, request: Request, response: Response) -> None:
}

payload = {}
if request.method in ('POST', 'PUT', 'PATCH', 'DELETE'):

if self.log_payload and request.method in {'POST', 'PUT', 'PATCH', 'DELETE'}:
try:
payload = await request.json()
except json.JSONDecodeError:
pass

if not isinstance(payload, dict):
payload = {
f"{type(payload).__name__}_payload_wrapper": payload
}
logging.warning("Failed to decode request payload as JSON, skipping logging.")
except Exception as e:
logging.error(f"Unexpected error while logging payload: {e}")
else:
if not isinstance(payload, dict):
payload = {
f"{type(payload).__name__}_payload_wrapper": payload
}

self.logger.log_struct(
info=payload,
Expand Down Expand Up @@ -202,6 +206,7 @@ def __init__(
self,
app: FastAPI,
request_logger_name: Optional[str] = None,
log_payload: bool = True,
*args, **kwargs
) -> None:
"""
Expand All @@ -225,7 +230,8 @@ def __init__(
name=request_logger_name or f"{os.getenv('GOOGLE_CLOUD_PROJECT')}{self.REQUEST_LOGGER_SUFFIX}",
resource=self.resource
),
resource=self.resource
resource=self.resource,
log_payload=log_payload
)
)
self.addFilter(LogInterceptor())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="fastapi-gae-logging",
version="0.0.3",
version="0.0.4",
description="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.",
Expand Down

0 comments on commit 4a4babd

Please sign in to comment.