用于日志记录的 django-sentry 有哪些轻量级替代品?

发布于 2024-11-30 16:35:12 字数 155 浏览 4 评论 0原文

在 Django 环境中是否有任何轻量级的替代方案可以替代 django-sentry 来记录错误?

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

谢谢。

Are there any lightweight alternatives to django-sentry for error logging in a Django environment?

I used django-db-log earlier which now known as django-sentry. Some of the others I found were pretty much dead as they had no commits in the last two years almost.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2024-12-07 16:35:12

Sentry 被过度杀伤,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)

Sentry being overkill and Djangodblog being deprecated, I rolled my own, cannibalising the necessary parts from both.

How it works is by catching the error signal. Then it uses Django's inbuilt exception reporter to generate the fancy 500 error page that Django displays when debugging is enabled. We store this in the DB and render it in the admin console.

Here's my implementation:

Model:

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)
焚却相思 2024-12-07 16:35:12

还有: http://pypi.python.org/pypi/django-logdb如果您想要更轻量但仍可生产的产品。

There is also: http://pypi.python.org/pypi/django-logdb if you want something more lightweight but still production ready.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文