2011-08-20 60 views
8

在Django環境中是否有任何輕量級的django-sentry替代錯誤日誌記錄?什麼是django-sentry的一些輕量級替代記錄?

我以前用django-db-log現在叫做django-sentry。我發現的其他一些人幾乎已經死了,因爲他們在過去的兩年中沒有任何犯罪。

謝謝。

+2

有沒有你不想使用內置記錄器的原因? https://docs.djangoproject.com/zh/dev/topics/logging/ – agf

+0

你需要什麼功能? – Spacedman

+0

最初我使用'django-db-log'這很好,因爲每當我在網站上發生錯誤時,我都會在管理面板中看到錯誤。我可以按類型,頻率等過濾錯誤頁面。它記錄的錯誤頁面是Django在所有堆棧跟蹤,內存中的變量,請求參數等遇到異常時顯示的默認500錯誤頁面。如果可以的話不用編寫大量代碼並使用Django的內部機制就可以做到這一點,那將會很棒。希望這是有幫助的解釋。謝謝。 –

回答

11

哨兵過大,Djangodblog被棄用,我推出了我自己的,搶食了兩個必要的部分。

工作原理是通過捕獲錯誤信號。然後它使用Django的內置異常記者來生成Django在啓用調試時顯示的花哨的500錯誤頁面。我們將其存儲在數據庫中並將其呈現在管理控制檯中。

這裏是我的實現:

型號:

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() 

初始化:

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) –