Sending Logs to GCP from Django DRF application

I am trying to access the logs from my django app in GCP logging. I have thus far been unsuccessful.

Here is my logging config:

 client = gcp_logging.Client.from_service_account_json(
        json_credentials_path='logging_service_account.json')

    client.setup_logging()

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {'format': '%(levelname)s : %(message)s - [in %(pathname)s:%(lineno)d]'},
            'short': {'format': '%(message)s'}
        },
        'handlers': {
            'stackdriver': {
                'formatter': 'standard',
                'class':  'google.cloud.logging.handlers.CloudLoggingHandler',
                'client': client
            },
            'requestlogs_to_stdout': {
                'class': 'logging.StreamHandler',
                'filters': ['request_id_context'],
            }
        },
        'filters': {
            'request_id_context': {
                '()': 'requestlogs.logging.RequestIdContext'
            }
        },
        'loggers': {
            'StackDriverHandler': {
                'handlers': ['stackdriver'],
                'level': "DEBUG"
            },
            'django.request': {
                'handlers': ['stackdriver']
            },
            'requestlogs': {
                'handlers': ['requestlogs_to_stdout'],
                'level': 'INFO',
                'propagate': False,
            },
        },
    }

I invoke the logs along the lines of:


import logging
logger = logging.getLogger('StackDriverHandler')
class OrganisationDetail(generics.RetrieveUpdateDestroyAPIView):
  ///

    def patch(self, request, pk, format=None):
        try:
            ///
            if serializer.is_valid():
                serializer.save()
                logger.info(f"PATCH SUCCESSFUL: {serializer.data}")
                return Response(serializer.data)
            logger.warning(f"PATCH Failed: {serializer.errors}")
            return JsonResponse(serializer.errors, status=400)
        except Exception as e:
            logger.error(f"PATCH Failed with exception: {e}")
            return JsonResponse({'error': str(e)}, status=500)

In GCP, I set up a service account, enabled logging api and gave the SA write logs and monitor metrics permissions.

I then made a secret to contain my service_account key, and in my cloud-build.yaml file I run a step like this:

- name: gcr.io/cloud-builders/gcloud
  entrypoint: 'bash'
  args: [ '-c', "gcloud secrets versions access latest --secret=<secret_name> --format='get(payload.data)' | tr '_-' '/+' | base64 -d > logging_service_account.json" ]

The above step should:

  1. Fetch the secret
  2. Write it to a json file in the app instance container that can be accessed by my settings.py file with gcp_logging.Client.from_service_account_json( json_credentials_path='logging_service_account.json')

Perhaps there is a more straight forward way to achieve this, but it feels like it should work. Any help would be much appreciated. Thanks

After all the steps above, when I visit the logging service on my gcp console, I only see the one log under my logging service account that says it is created, none of the logs from my actual django app.

Back to Top