2

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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

The documentation does not mention how to log the request and response. Could you pease share some example?
@AmolChakane I am not sure what you mean but if you are talking about the second option for which I linked a page in the docs, then the response is fetched inside the custom handler and you can log it anyhow you wish. As for the request, your initial question doesn't mention that but if you want to log it, then use the middleware option as it handles both requests and responses
My initial question mentions the logging request. I was able to log the request using custom exception handler. Thanks a ton!
Oh, I just noticed that your questions mentions the request as well. I am glad you were able to solve it. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.