python Django未处理的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1925898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django Unhandled Exception
提问by Hyman
It is running under DEBUG = True mode. Sometimes it can throw out an error message with traceback information when encounter an error but sometimes it just display the following lines:
它在 DEBUG = True 模式下运行。有时它会在遇到错误时抛出带有回溯信息的错误消息,但有时它只显示以下几行:
Unhandled Exception
An unhandled exception was thrown by the application.
I have to switch to development server to see detail message.
我必须切换到开发服务器才能查看详细消息。
How can I make it always display traceback message when an error is encountered?
遇到错误时,如何使其始终显示回溯消息?
采纳答案by diegueus9
Maybe you can use this snippet, this will log exceptions in apache's log:
也许您可以使用此代码段,这将在 apache 的日志中记录异常:
utils.py
:
utils.py
:
def log_traceback(exception, args):
import sys, traceback, logging
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
logging.debug(exception)
logging.debug(args)
for tb in traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback):
logging.debug(tb)
site_logging.py
:
site_logging.py
:
import logging
import sys
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
Put it in your settings.py
:
把它放在你的settings.py
:
import site_logging
And in your code:
在你的代码中:
from where.is.your.utils import log_traceback
try:
`do something`
except Exception, args:
log_traceback(Exception, args)
回答by stephane k.
Just connect to the got_request_exception signal and log the exception:
只需连接到 got_request_exception 信号并记录异常:
from django.core.signals import got_request_exception
import logging
def log(*args, **kwargs):
logging.exception('error')
got_request_exception.connect(log)
This will log the whole trace. In the dev server, it logs in the console.
这将记录整个跟踪。在开发服务器中,它登录到控制台。
回答by Ralph Willgoss
Are you using Apache?
Just out of interest is this your Production or Dev environment where you want to see the traceback?
你在用阿帕奇吗?
只是出于兴趣,这是您想要查看回溯的生产或开发环境吗?
From the DJango Book on security - Exposed error messages
来自DJango Book on security - Exposed 错误消息
Users deploying under Apache and mod_python should also make sure they have PythonDebug Off in their Apache conf files; this will ensure that any errors that occur before Django's had a chance to load won't be displayed publicly.
在 Apache 和 mod_python 下部署的用户还应该确保他们的 Apache conf 文件中有 PythonDebug Off;这将确保在 Django 有机会加载之前发生的任何错误都不会公开显示。
I assume you want PythonDebug On, this is recommended for Development only.
我假设你想要 PythonDebug On,这仅推荐用于开发。
回答by Matthew Schinckel
That's what DEBUG=True is for: to show the full traceback. The idea is that regular users do not want (nor do you want them to) see anything other than a simple error message.
这就是 DEBUG=True 的用途:显示完整的回溯。这个想法是普通用户不希望(也不希望他们)看到除了简单的错误消息之外的任何内容。