0

在我的一个观点中,我处理了一个一般的 python 异常。此异常不会使站点崩溃,也不会导致 500。

我想用 django 记录这个异常,以便我可以检查它。什么是 pythonic 和 djangonic [??] 为什么要这样做?

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')
4

1 回答 1

1

标准的日志记录模块提供了你所需要的一切——在这种情况下,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")

settings.py然后,您只需按照 Django 的 FineManual(tm) 中的说明正确配置您的记录器。

于 2013-05-23T14:38:43.260 回答