Enhance Your Django REST API with Google Cloud Logging
In this tutorial I will show you how to implement Google Cloud Logging to your Django APP, specifically for a DRF (Django Rest Framwork) project.
I assume that you already have a Google Cloud account enabled, if not, it is very easy to set up, and they offer a good free trial with enough credits to start your projects.
Garanting Permissions
First of all, you need to grant your service account permission to write logs in Cloud Logging. Go to the IAM menu and add the “Logs Writer” permission role to your service account.
After this, enable the Cloud Logging API, in “APIs & Services” menu to ensure access from your DRF app.
Download the credentials.json file from your service account to authenticate your access from localhost.
Ensure to not lose this file, you can’t download the same key after again.
If you is looking to learn how to deal with logs in your django app, I assume you already know how to start your DRF project. Otherwise, you can look to official DRF documentation to start your project.
In this section, I will explain how to set all details in your settings.py file, and all another required stuffs.
Installing dependencies
pip install google-cloud-logging
pip install python-dotenv
Save this libraries in your requirements.txt file, to future uses or deploy. In this tutorial I’m using this versions: google-cloud-logging==3.10.0 and python-dotenv==1.0.1
Setting your project
You need to add this code to your settings file. It’s preferable to add this code at the start of your settings.py
:
import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler
from google.cloud.logging import Client
import logging
from dotenv import load_dotenv
load_dotenv()
client = Client()
cloud_handler = CloudLoggingHandler(client)
logging.getLogger().addHandler(cloud_handler)
logging.getLogger().setLevel(logging.INFO)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'cloud_logging': {
'class': 'google.cloud.logging.handlers.CloudLoggingHandler',
'client': google.cloud.logging.Client(),
},
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console', 'cloud_logging'],
'level': 'INFO',
'propagate': True,
},
'__main__': {
'handlers': ['console', 'cloud_logging'],
'level': 'INFO',
'propagate': True,
},
},
}
Add the credentials.json
file to the project root and create a .env
file with this content:
GOOGLE_APPLICATION_CREDENTIALS="credentials.json"
This variable should contain the path to your credentials file. In this case, I added the credentials.json
to the project root.
Creating a Test Case
In this section, I will create a view to receive a GET request and write the log to Google Cloud Logging.
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
import logging
logger = logging.getLogger('fidc-credit-assignment.assignment')
# Create your views here.
class TestView(APIView):
def get(self, request):
logger.error({
"message": "Debug Django APP",
"request": request.data
})
return Response(data={"message": "Log succesfully registred!"})
Remember to add the view in urls.py file, to access the route.
Testing
When you finish setting up your project, make a request to the route and watch the magic happen. Just open your Google Cloud Logging terminal, go to “Logs Explorer”, and you will see a log with the information that you sent.
If you want, access the source code to understand better all the proccess and don’t forget to add your credentials.json file.
Use Cases
- Exception Handler: Django Rest Framework allows you to set a custom default Exception Handler. You can configure it to always send a log to your Cloud Logging.
- Custom Dashboards on Cloud Logging: You can set a default message in your logs and create custom views in Cloud Logging to monitor specific processes that you implement. It’s a great idea to use this for monitoring queues, for example.
- Create Alerts on Cloud Logging: You can create alerts in Cloud Logging for specific errors and schedule actions to be taken in some error cases.