9

Django のエラー レポートをカスタマイズして、エラーを引き起こしたユーザーをメールで知らせてくれる方法はありますか?

問題があれば、私は Django 1.2 を使用しています。

よろしくお願いします!

4

2 に答える 2

14

セントリーを使用したくない場合は、この単純なミドルウェアを使用して、ユーザー情報をエラー メールに添付できます。

# source: https://gist.github.com/646372
class ExceptionUserInfoMiddleware(object):
    """
    Adds user details to request context on receiving an exception, so that they show up in the error emails.

    Add to settings.MIDDLEWARE_CLASSES and keep it outermost(i.e. on top if possible). This allows
    it to catch exceptions in other middlewares as well.
    """

    def process_exception(self, request, exception):
        """
        Process the exception.

        :Parameters:
           - `request`: request that caused the exception
           - `exception`: actual exception being raised
        """

        try:
            if request.user.is_authenticated():
                request.META['USERNAME'] = str(request.user.username)
                request.META['USER_EMAIL'] = str(request.user.email)
        except:
            pass

このクラスを Django プロジェクトの下の任意の *.py ファイルに配置し、への参照を追加するだけMIDDLEWARE_CLASSESです。つまり、プロジェクト ルート (settings.py がある場所) のファイル "middleware" に配置する場合は、単純にmiddleware.ExceptionUserInfoMiddleware.

于 2011-07-13T15:25:39.007 に答える
2

仕事にはhttp://readthedocs.org/docs/sentry/en/latest/index.htmlを強くお勧めします。

于 2011-07-13T15:17:20.990 に答える