Django未处理的异常 [英] Django Unhandled Exception

查看:135
本文介绍了Django未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它正在DEBUG = True模式下运行。有时候,当遇到错误时,它可能会抛出一条带有追溯信息的错误消息,但有时只显示以下行:

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:

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?

推荐答案

也许您可以使用这个代码片段将记录apache日志中的例外情况:

Maybe you can use this snippet, this will log exceptions in apache's log:

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

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)

将其放在您的 settings.py

import site_logging

在你的代码中:

from where.is.your.utils import log_traceback
try:
   `do something`
except Exception, args:
    log_traceback(Exception, args)

这篇关于Django未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆