2011-08-20 23 views
8

Có bất kỳ lựa chọn thay thế nhẹ nào cho django-sentry để đăng nhập lỗi trong môi trường Django không?Một số lựa chọn thay thế nhẹ cho django-sentry để đăng nhập là gì?

Tôi đã sử dụng django-db-log trước đó hiện được gọi là django-sentry. Một số người khác tôi đã tìm thấy đã chết khá nhiều vì họ không có cam kết trong hai năm qua gần như.

Cảm ơn.

+2

Có lý do nào bạn không muốn sử dụng trình ghi nhật ký dựng sẵn không? https://docs.djangoproject.com/en/dev/topics/logging/ – agf

+0

bạn cần chức năng nào? – Spacedman

+0

Ban đầu tôi đã sử dụng 'django-db-log' rất tốt vì mỗi khi tôi gặp lỗi trên trang web, tôi có thể thấy lỗi trong bảng quản trị. Tôi có thể lọc lỗi theo loại, tần số, vv Trang lỗi mà nó đăng nhập là trang lỗi 500 mặc định mà Django hiển thị khi ngoại lệ gặp phải với tất cả stacktraces, biến trong bộ nhớ, yêu cầu thông số, v.v. Nếu tôi có thể làm tương tự mà không cần viết nhiều mã và sử dụng cơ chế nội bộ của Django, điều đó sẽ rất tuyệt. Hy vọng đây là lời giải thích hữu ích. Cảm ơn. –

Trả lời

11

Sentry quá mức cần thiết và Djangodblog không còn được dùng nữa, tôi đã tự mình cuộn, phân phối các phần cần thiết từ cả hai.

Cách hoạt động bằng cách bắt tín hiệu lỗi. Sau đó, nó sử dụng phóng viên ngoại lệ sẵn có của Django để tạo ra trang lỗi 500 lỗi mà Django hiển thị khi gỡ lỗi được kích hoạt. Chúng tôi lưu trữ điều này trong DB và hiển thị nó trong bảng điều khiển dành cho quản trị viên.

Dưới đây là thực hiện của tôi:

mẫu:

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) 

Admin:

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) 

Helper:

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

Điều này hiện có sẵn dưới dạng gói từ PyPi. http://pypi.python.org/pypi/django-erroneous/ –

+1

Câu hỏi và câu trả lời này đã thúc đẩy tôi tự quay mình. Tôi đã sử dụng nó trong sản xuất một thời gian và cuối cùng đã phát hành nó trên pypi là tốt: https://pypi.python.org/pypi/django-shoogie/ –

+1

Đối với Django> = 1,4, một "form_url" arg là được thêm vào phương thức change_view() của ModelAdmin. Điều này có nghĩa là mã trên cần một sửa đổi nhỏ để vẫn hoạt động. Trong lớp ErrorAdmin ở trên, trong phương thức change_view() được ghi đè, lệnh gọi super() về phía cuối phương thức sẽ chỉ định extra_context làm đối số từ khóa, như sau: return super (ErrorAdmin, self) .change_view (request, object_id, extra_context = extra_context) –