0

In one of my views, I handle a general python exception. This exception does not crash the site, or result in a 500.

I would like to log this exception with django so that I can inspect it. What is the pythonic and djangonic [??] why to do this?

def Set( request ):
    if request.POST:
         try:
             #something nearly impossible
         except Exception as exc:
             #where do I log this?
             pass

    return HttpResponse(simplejson.dumps({'ya':'hoo!'}), mimetype='application/json')
2
  • The built-in standard Python logging doesn't work in django? docs.python.org/2.7/library/logging.html Commented May 23, 2013 at 13:54
  • sentry and raven play nice Commented May 23, 2013 at 14:21

1 Answer 1

1

The standard logging module provides all you need - in this case, logger.exception():

import logging
logger = logging.getLogger("your-logger-name")

def Set(request):
    if request.method == "POST":
        try:
            #something nearly impossible
        except Exception as exc:
            #where do I log this?
            logger.exception("some exception message")

Then you just have to properly configure your logger in your settings.py as documented in Django's FineManual(tm).

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

2 Comments

Thank you... But how to get the full stack trace?
logger.exception does log the whole thing - that's the point.

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.