Created
November 8, 2014 15:49
-
-
Save Tukki/f7f64dd5298be514d171 to your computer and use it in GitHub Desktop.
django handler500 示例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: home/moo/workspace/django-trunk/docs/request_response.txt | |
=================================================================== | |
--- /home/moo/workspace/django-trunk/docs/request_response.txt (revision 6657) | |
+++ /home/moo/workspace/django-trunk/docs/request_response.txt (working copy) | |
@@ -588,3 +588,29 @@ | |
That takes care of setting ``handler500`` in the current module. As you can see | |
in ``django/conf/urls/defaults.py``, ``handler500`` is set to | |
``'django.views.defaults.server_error'`` by default. | |
+ | |
+Here is example how to log the exception using Python logging API and | |
+display an error page on the production server:: | |
+ | |
+ from django.http import HttpResponseServerError | |
+ import sys | |
+ | |
+ def handle_exception(request, *args, **kwargs): | |
+ """ Custom exception handler for Django. | |
+ | |
+ Note that settings.DEBUG must be False or | |
+ this handler is never run. | |
+ """ | |
+ | |
+ # Get the latest exception from Python system service | |
+ exception = sys.exc_info()[0] | |
+ | |
+ # Use Python logging module to log the exception | |
+ # For more information see: | |
+ # http://docs.python.org/lib/module-logging.html | |
+ logger.error("Uncaught exception got through, rendering 500 page") | |
+ logger.exception(exception) | |
+ | |
+ # Output user visible HTTP response | |
+ return HttpResponseServerError(render_to_string("pages/500.html")) | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment