djangologgingerror-handlingdjango-rest-frameworkdjango-logging

How to log every request and response if 500 error occurs in django rest framework?


I am using django for rest APIs.

I do have a logging code in settings.py.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(asctime)s %(levelname)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(asctime)s %(levelname)s %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': '/tmp/OAuthLog_Production.log',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'oauth': {
            'handlers': ['file'],
            'level': 'ERROR',
            'propagate': True,
        }
    },
}

In the views.py it is being used like this whenever any exception occurs.

import logging
logger = logging.getLogger("oauth")
      except Exception as ex:
            logger.exception(ex)
            response = helper.generateStandardResponse(ResponseCodes.exception_in_finding_user,
                                                       "Exception in finding user", data, False);
            logger.info("get_profile_details : Exception in finding user")
            return Response(response, status=status.HTTP_200_OK)

Since, I am using Elastic beanstalk to host the application I am not returning 500 status code. I am returning custom responses.

Even after this there are few 500 errors returned by application. And those are causing environment to "Degrade".

I checked my log file but it has errors like this.

2019-11-23 23:53:10,600 ERROR UserProfile matching query does not exist.
Traceback (most recent call last):
  File "/opt/python/current/app/profiles/views.py", line 514, in get_profile_details
    user_profile_detail = UserProfile.objects.get(mobile1=mobile, account_type=account_type)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/models/query.py", line 385, in get
    self.model._meta.object_name
profiles.models.DoesNotExist: UserProfile matching query does not exist.

I am not able to understand why there are 500 errors?

How can I log each and every request and response which is causing 500 errors?

So that I can debug and find out what input and client is causing the issues and resolve them.


Solution

  • You can write a middleware to catch all uncaught exceptions and return appropriate responses or implement custom exception handling as described here in DRF docs