FAQ | This is a LIVE service | Changelog

Django logging middleware and trace id bound to logger

Description

Add Django middleware to log every request and to ensure all requests include a unqie ID that ties logs together for each request.

Reference to example middleware logging (not structlog based):

https://gitlab.developers.cam.ac.uk/uis/devops/iam/card-database/card-api/-/blob/master/project/middleware.py?ref_type=heads

Acceptance criteria

  • middleware logs all requests start and end when middleware including in settings
    • method
    • paths
    • status
    • duration
  • GCP trace ID or manually generated value if missing bound to loggers
  • get_structlog_logger method added to library (returns logger from structlog.get_logger, bound when in a request context)
  • align any new fields with GCP special field names (those within current scope). E.g. "message" vs "event" https://cloud.google.com/logging/docs/structured-logging
  • if django_structlog is not used and no other django specialised dependencies are needed, remove [django] extra/optional package mechanisms.
Edited by Ed Kirk