PHACDataHub / cloudrun-deployment-example

Working towards deploying Django apps to Google Cloud Run, using a Cloud SQL Postgres database and a Cloud Build GitHub Trigger.
0 stars 0 forks source link

Add structured logging to Django #15

Closed sleepycat closed 1 year ago

sleepycat commented 1 year ago

Cloud providers like Google offer really powerful tools to aggregate logs and search through them. Modern logging libraries use structured logging (basically logging out JSON instead of a single line of unstructured text). We'll need to add a structured logging library to Django so that we can get the benefits of GCPs logging capabilities.

LilaKelland commented 1 year ago

Added logging from google.cloud and structlog packages. Logs are called from views.py - and are neither sophisticated nor pretty. (PR Add structured logging https://github.com/PHACDataHub/cloudrun-deployment-example/pull/23). I imagine we want to tweak this a bit before we call this closed? image

Stephen-ONeil commented 1 year ago

To work, the string sent to stdout (or otherwise sent to the GCP logs) needs to parse as valid JSON. The "INFO:helloworld.views:" that's prepended to the JSON string is at fault here, so it fell back to just being treated as a textPayload instead of a jsonPayload in the logs.

Looked around in your commit but didn't see a clear culprit. I'm going to try taking a run at integrating structlog (and some other logging nice-to-haves) over on CPHO. If that works well, I'll spin it off in to something reusable some time :+1:

Stephen-ONeil commented 1 year ago

Completed in the CPHO repo: https://github.com/PHACDataHub/cpho-phase2/pull/69

I've got a pretty advanced logging configuration in there. If it's accepted well in CPHO, I'm expecting it'll end up easy to reuse, either via the PHAC Django helpers library or a standalone package.