How to correlate and aggregate logs in Google App Engine Python 3?

TL;DR: Has anyone got log correlation / aggregation working in App Engine Python 3?

In order to get our App Engine application logs to correlate and aggregate all logs from a single request into one collapsed log entry in the Google Cloud Console Logs Explorer, seemingly we need to use the Google Cloud Logging libraries (which interestingly add over 15MB to the deployed app size).

There are half-baked docs and examples which point to each other but never really properly show how to do this:

https://cloud.google.com/appengine/migration-center/standard/python/migrate-to-cloud-logging

https://cloud.google.com/logging/docs/view/correlate-logs

https://cloud.google.com/logging/docs/setup/python

https://cloud.google.com/appengine/docs/standard/writing-application-logs?tab=python#related-app-log...

There seems to also be a potential bug that has been lingering for years:

https://issuetracker.google.com/issues/138365527

In some of the above resources, apparently you need to call logger.setup_client() to get the logs correlated, however there is never any example of what "logger" actually is.

There is a client.setup_logging() call which we are already using, however this seems to merely make the log severity visible in the logs, but does not correlate the logs.

Has anyone got this working?

0 3 126
3 REPLIES 3

Hi @daskalou,

Welcome to Google Cloud Community!

Correlating logs in App Engine Python 3 requires the Google Cloud Logging library, which increases deployment size.

  • Use logger.setup_client() for proper correlation. Call it before request handling.
  • Alternatively, add request context (like trace ID) to logs for basic filtering.
  • Consider external logging services for advanced aggregation if size is a concern.

Reference:

You can use the `StructuredLoggingMiddleware` from my App Engine afterburner utility (https://github.com/firi/appengine-afterburner/tree/main), that enables request log correlation by writing structured logs to stdout. No additional dependencies required beyond the App Engine builtin services.

I wrote up what worked in our migration experience at https://gae123.com/article/py3logging