| 591 | |
| 592 | Here is example how to log the exception using Python logging API and |
| 593 | display an error page on the production server:: |
| 594 | |
| 595 | from django.http import HttpResponseServerError |
| 596 | import sys |
| 597 | |
| 598 | def handle_exception(request, *args, **kwargs): |
| 599 | """ Custom exception handler for Django. |
| 600 | |
| 601 | Note that settings.DEBUG must be False or |
| 602 | this handler is never run. |
| 603 | """ |
| 604 | |
| 605 | # Get the latest exception from Python system service |
| 606 | exception = sys.exc_info()[0] |
| 607 | |
| 608 | # Use Python logging module to log the exception |
| 609 | # For more information see: |
| 610 | # http://docs.python.org/lib/module-logging.html |
| 611 | logger.error("Uncaught exception got through, rendering 500 page") |
| 612 | logger.exception(exception) |
| 613 | |
| 614 | # Output user visible HTTP response |
| 615 | return HttpResponseServerError(render_to_string("pages/500.html")) |
| 616 | No newline at end of file |