2011-08-20 3 views
8

Django環境でのエラーログのために、軽量代替django-sentryはありますか?django-sentryの軽量代替手段は何ですか?

として現在知られているdjango-db-logを使用しました。私が見つけたもののいくつかは、過去2年間にコミットしていなかったので、かなり死んでいました。

ありがとうございました。

+2

組み込みのロガーを使用したくない理由はありますか? https://docs.djangoproject.com/ja/dev/topics/logging/ – agf

+0

どのような機能が必要ですか? – Spacedman

+0

当初私は 'django-db-log'を使用していましたが、これはサイトでエラーが発生するたびに管理パネルにエラーが表示されるためです。エラーをタイプ、頻度などでフィルタリングすることができます。エラーページは、Djangoがすべてのスタックトレース、メモリ内の変数、リクエストパラメータなどで例外が発生したときに表示されるデフォルトの500エラーページでした。多くのコードを書かずに、Djangoの内部メカニズムを使うことなく同じことをしてください。これは参考になる説明です。ありがとう。 –

答えて

11

センテリーは過度であり、Djangodblogは非難されています。私は自分自身を転がし、両方から必要な部分を食い止めました。

どのように動作するかは、エラー信号をキャッチすることです。次に、Djangoの組み込み例外レポータを使用して、デバッグが有効になっているときにDjangoが表示する空白の500エラーページを生成します。これをDBに保存し、管理コンソールに表示します。ここで

は私の実装です:

モデル:

class Error(Model): 
    """ 
    Model for storing the individual errors. 
    """ 
    kind = CharField(_('type'), 
     null=True, blank=True, max_length=128, db_index=True 
    ) 
    info = TextField(
     null=False, 
    ) 
    data = TextField(
     blank=True, null=True 
    ) 
    path = URLField(
     null=True, blank=True, verify_exists=False, 
    ) 
    when = DateTimeField(
     null=False, auto_now_add=True, db_index=True, 
    ) 
    html = TextField(
     null=True, blank=True, 
    ) 

    class Meta: 
     """ 
     Meta information for the model. 
     """ 
     verbose_name = _('Error') 
     verbose_name_plural = _('Errors') 

    def __unicode__(self): 
     """ 
     String representation of the object. 
     """ 
     return "%s: %s" % (self.kind, self.info) 

管理者:

class ErrorAdmin(admin.ModelAdmin): 
    list_display = ('path', 'kind', 'info', 'when') 
    list_display_links = ('path',) 
    ordering  = ('-id',) 
    search_fields = ('path', 'kind', 'info', 'data') 
    readonly_fields = ('path', 'kind', 'info', 'data', 'when', 'html',) 
    fieldsets  = (
     (None, { 
      'fields': ('kind', 'data', 'info') 
     }), 
    ) 

    def has_delete_permission(self, request, obj=None): 
     """ 
     Disabling the delete permissions 
     """ 
     return False 

    def has_add_permission(self, request): 
     """ 
     Disabling the create permissions 
     """ 
     return False 

    def change_view(self, request, object_id, extra_context={}): 
     """ 
     The detail view of the error record. 
     """ 
     obj = self.get_object(request, unquote(object_id)) 

     extra_context.update({ 
      'instance': obj, 
      'error_body': mark_safe(obj.html), 
     }) 

     return super(ErrorAdmin, self).change_view(request, object_id, extra_context) 

admin.site.register(Error, ErrorAdmin) 

ヘルパー:

class LoggingExceptionHandler(object): 
    """ 
    The logging exception handler 
    """ 
    @staticmethod 
    def create_from_exception(sender, request=None, *args, **kwargs): 
     """ 
     Handles the exception upon receiving the signal. 
     """ 
     kind, info, data = sys.exc_info() 

     if not issubclass(kind, Http404): 

      error = Error.objects.create(
       kind = kind.__name__, 
       html = ExceptionReporter(request, kind, info, data).get_traceback_html(), 
       path = request.build_absolute_uri(), 
       info = info, 
       data = '\n'.join(traceback.format_exception(kind, info, data)), 
      ) 
      error.save() 

INIT:

from django.core.signals import got_request_exception 

from modules.error.signals import LoggingExceptionHandler 

got_request_exception.connect(LoggingExceptionHandler.create_from_exception) 
+0

これはPyPiからパッケージとして入手できるようになりました。 http://pypi.python.org/pypi/django-erroneous/ –

+1

この質問と回答は、私自身もロールするよう動機づけました。私はその製品をしばらく使用していて、ついにそれをpypiでもリリースしました:https://pypi.python.org/pypi/django-shoogie/ –

+1

Django> = 1.4の場合、 "form_url" argはModelAdminのchange_view()メソッドに追加されました。つまり、上記のコードではまだ動作するには少し修正が必要です。上記のErrorAdminクラスでは、オーバーライドされたchange_view()メソッドで、メソッドの終わりに向かってsuper()を呼び出すと、次のようにextra_contextをキーワード引数として指定する必要があります。return super(ErrorAdmin、self).change_view(request、 object_id、extra_context = extra_context) –

関連する問題