2016-03-22 4 views
4

Django 1.9のエラーメールは以前よりずっと長くなっているようです。私は余分であり、潜在的にあまりにも明らかであると思う "設定"のセクション全体があります。Djangoのエラーメールが長すぎます。どのように切り詰めるのですか?

Djangoが送信するエラーメールを編集する最適な方法は何ですか?

編集:私は機密情報を隠そうとしているだけではありません。 Django 1.9の電子メールにはさらに多くのコンテンツがあり、電子メールの形式をより短く変更したいと考えています。私はそれが古い方法が好きだった。

+0

def get_traceback_data(self):を貼り付けコピーした場合、あなたがここを見てなかった、あなたは'settings': get_safe_settings(),ライン294をコメントアウトすることができます設定を非表示にする場合:[HTTPS://docs.djangoproject。 com/es/1.9/howto /エラー報告/#フィルタに敏感な情報](https://docs.djangoproject.com/es/1.9/howto/error-reporting/#filtering- sensitive-information) – niklas

+0

また、あなたのために物事をあなたのために置く:https://docs.djangoproject.com/ja/1.9/ref/settings/#debug - API、TOKEN、KEY、SECRET、PASS、またはSIGNATUREを含むすべてのトークンが自動的に隠される – karthikr

+0

2つのキューがありますあなたと同じように私が見つけたstions:http://stackoverflow.com/questions/27411362/django-error-reporting-emails-env-vars-leak-info and http://stackoverflow.com/questions/12301105/django- 1-3エラーレポートの削除に敏感な情報?rq = 1これは@niklasの説明につながります。 –

答えて

4

TECHNICAL_500_TEMPLATE/TECHNICAL_500_TEXT_TEMPLATEdjango debug viewにあります。この変数は、エラー報告に表示される内容やエラーメールを制御します。コメントは、テンプレートローダーが壊れた場合にエラーが生成されるように、テンプレートがPython変数にあることを説明しています。あなたはあなたのdjangoパッケージでこの変数を変更することができますが、私はそれをお勧めしません。 TECHNICAL_500_TEMPLATEは、同じファイル内のExceptionReporterクラスによって参照されます。

AdminEmailHandlerdjango utils logは、ExceptionReporterを使用してhtmlエラーレポートを生成します。

あなたはAdminEmailHandlerをサブクラス化し、独自のTECHNICAL_500_TEMPLATEを定義し使用していますExceptionReporterのあなたのサブクラス化バージョンを含めるようにemit関数をオーバーライドすることができます。

はここに例を示します

from copy import copy 

from django.views import debug 
from django.utils import log 
from django.conf import settings 
from django import template 

TECHNICAL_500_TEMPLATE = """ 
    # custom template here, copy the original and make adjustments 
""" 
TECHNICAL_500_TEXT_TEMPLATE = """ 
    # custom template here, copy the original and make adjustments 
""" 

class CustomExceptionReporter(debug.ExceptionReporter): 
    def get_traceback_html(self): 
     t = debug.DEBUG_ENGINE.from_string(TECHNICAL_500_TEMPLATE) 
     c = template.Context(self.get_traceback_data(), use_l10n=False) 
     return t.render(c) 

    def get_traceback_text(self): 
     t = debug.DEBUG_ENGINE.from_string(TECHNICAL_500_TEXT_TEMPLATE) 
     c = template.Context(self.get_traceback_data(), autoescape=False, use_l10n=False) 
     return t.render(c) 

class CustomAdminEmailHandler(log.AdminEmailHandler): 
    def emit(self, record): 
     try: 
      request = record.request 
      subject = '%s (%s IP): %s' % (
       record.levelname, 
       ('internal' if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS 
       else 'EXTERNAL'), 
       record.getMessage() 
      ) 
     except Exception: 
      subject = '%s: %s' % (
       record.levelname, 
       record.getMessage() 
      ) 
      request = None 
     subject = self.format_subject(subject) 

     no_exc_record = copy(record) 
     no_exc_record.exc_info = None 
     no_exc_record.exc_text = None 

     if record.exc_info: 
      exc_info = record.exc_info 
     else: 
      exc_info = (None, record.getMessage(), None) 

     reporter = CustomExceptionReporter(request, is_email=True, *exc_info) 
     message = "%s\n\n%s" % (self.format(no_exc_record), reporter.get_traceback_text()) 
     html_message = reporter.get_traceback_html() if self.include_html else None 
     self.send_mail(subject, message, fail_silently=True, html_message=html_message) 

reporter.pyを作成してからちょうどlogging sectionに新しいハンドラを使用するようにDjangoの設定を行います。

LOGGING = { 
    # Your other logging settings 
    # ... 
    'handlers': { 
     'mail_admins': { 
      'level': 'ERROR', 
      'class': 'project.reporter.CustomAdminEmailHandler', 
      'filters': ['special'] 
     } 
    }, 
} 

あなたはちょうどあなたがオーバーライドしてCustomExceptionReporter

関連する問題