有哪些轻量级的替代方案可以用于django-sentry日志记录?

9

在Django环境中,是否有轻量级的替代品可以用于错误日志记录,而不是使用django-sentry

我之前使用过django-db-log,现在已被更名为django-sentry。其他一些替代品似乎已经死亡,因为它们几乎两年没有提交了。

谢谢。


2
你不想使用内置的日志记录器有什么原因吗?https://docs.djangoproject.com/en/dev/topics/logging/ - agf
你需要什么功能? - Spacedman
最初我使用的是 django-db-log,因为每次在网站上出现错误时,我都可以在管理面板中看到错误。我可以按类型、频率等过滤错误。它记录的错误页面是 Django 在遇到异常时显示的默认 500 错误页面,其中包含所有堆栈跟踪、内存中的变量、请求参数等。如果我能够不写太多代码并使用 Django 的内部机制来实现同样的功能,那就太好了。希望这个解释有帮助。谢谢。 - Mridang Agarwalla
嗨,Agf和Spacedman,如果你们感兴趣的话,我已经在下面的答案中添加了我的实现。 - Mridang Agarwalla
2个回答

13

Sentry过于复杂,而Djangodblog已经被弃用,于是我自己开发了一个,从两者中提取了必要的部分。

它的工作原理是通过捕获错误信号来实现。然后它使用Django内置的异常报告器生成精美的500错误页面,在调试模式下Django会显示此页面。我们将这个页面存储在数据库中,并在管理控制台中呈现出来。

这是我的实现:

模型:

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
现在,您可以从PyPi获取此软件包。http://pypi.python.org/pypi/django-erroneous/ - Mridang Agarwalla
1
这个问题和答案激励我自己动手做了一个。我已经在生产中使用它一段时间了,最终也在pypi上发布了:https://pypi.python.org/pypi/django-shoogie/ - Aryeh Leib Taurog
1
对于Django >= 1.4,ModelAdmin的change_view()方法添加了一个“form_url”参数。这意味着上面的代码需要稍作修改才能正常工作。在上面的ErrorAdmin类中,重写的change_view()方法中,方法末尾对super()的调用现在应该指定extra_context作为关键字参数,像这样:return super(ErrorAdmin, self).change_view(request, object_id, extra_context=extra_context)。 - Christian Abbott

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接