6

I have a specific view in my app which should return HttpResponse('') if everything was done successfully and smth like HttpResponseBadRequest() otherwise.

This view works with external data so some unexpected exceptions can be raised. Of course I need to know what happened. So I have smth like that:

def my_view(request):
    try:
        # a lot of code
        return HttpResponse('')
    except:
        import logging
        logger = logging.getLogger('django.request')
        # logger.error(extra={'request': request}) or logger.exception()
        return HttpResponseBadRequest('')

I have a default logging config if it matters (django 1.5).

logger.exception sends only a small traceback and no request data. logger.error sends only request data.

So the question is how to get exactly the same traceback as django sends (with the same subject/body)). I think there must be some clean and simple solution that i just can't find.

UPDATE

So with the patched exception method i ended up with the following code:

logger.exception('Internal Server Error: %s', request.path,
                 extra={'status_code': 500, 'request': request})

which produces equal email traceback as built-in django logging.

2
  • can you show us your django logging config (from settings file)? Commented Sep 30, 2013 at 21:37
  • I use default logging (don't set my own LOGGING in settings.py). Anyway I got my answer (see update). Commented Sep 30, 2013 at 23:17

1 Answer 1

3

This problem is due to a bug which has been fixed in recent versions of Python 2.7. This bug meant that the exception method did not accept the extra argument.

If you can't upgrade to a suitably recent Python 2.7, then you can perhaps patch your Python with the fix referenced in the issue I've linked to. If you can't do that, you'll need to subclass Logger and override the exception method to do the right thing (which is basically to add a **kwargs to the exception method's signature, which is passed through to its call to the error method.

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

1 Comment

Thanks. One of those things that can be expected (I mean python bug). Unfortunately my OS is ubuntu 12.04 which has python 2.7.3 on board and it can't be updated to 2.7.5 easily. But I've manually patched built-in logging module and now everything works fine. See my update.

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.