什么是django-sentry用于伐木的轻量级替代品?

时间:2011-08-20 09:44:25

标签: python django sentry

Django环境中的错误记录是否有django-sentry的轻量级替代品?

我之前使用django-db-log,现在称为django-sentry。我发现的其他一些人几乎死了,因为他们在过去两年几乎没有任何承诺。

感谢。

2 个答案:

答案 0 :(得分:12)

哨兵过度杀戮,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)

答案 1 :(得分:2)

还有:http://pypi.python.org/pypi/django-logdb如果你想要更轻量级的东西,但仍然可以生产。