title: How to log every request and response if 500 error occurs in django rest framework?
body: <p>I am using django for rest APIs. </p>
  
  <p>I do have a logging code in settings.py.</p>
  
  <pre><code>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,
          }
      },
  }
  </code></pre>
  
  <p>In the views.py it is being used like this whenever any exception occurs.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Since, I am using Elastic beanstalk to host the application I am not returning 500 status code.
  I am returning custom responses.</p>
  
  <p>Even after this there are few 500 errors returned by application.
  And those are causing environment to <strong>"Degrade"</strong>.</p>
  
  <p>I checked my log file but it has errors like this.</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>I am not able to understand why there are 500 errors?</p>
  
  <p>How can I log each and every request and response which is causing 500 errors?</p>
  
  <p>So that I can debug and find out what input and client is causing the issues and resolve them.</p>