9

django-sentryDjango 環境でのエラー ログの軽量な代替手段はありますか?

以前は として使用django-db-logしていましたが、現在は として知られていdjango-sentryます。私が見つけた他のいくつかは、過去2年間ほとんどコミットしていなかったため、ほとんど死んでいた.

ありがとう。

4

2 に答える 2

12

Sentry はやり過ぎで、Djangodblog は非推奨なので、両方から必要な部分を共食いして、独自のロールを作成しました。

それがどのように機能するかは、エラー信号をキャッチすることです。次に、Django の組み込みの例外レポーターを使用して、デバッグが有効になっているときに Django が表示する派手な 500 エラー ページを生成します。これを DB に保存し、管理コンソールでレンダリングします。

これが私の実装です:

モデル:

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)
于 2011-09-28T07:05:26.690 に答える
2

また、http://pypi.python.org/pypi/django-logdbもあります。より軽量でありながら本番環境に対応したい場合。

于 2011-11-27T23:00:12.400 に答える