记录过滤器操纵什么类型的对象?

时间:2014-09-17 13:51:37

标签: python django

我目前正在仔细研究Django logging filters 在他们的一个例子中,他们定义了这种过滤方法:

from django.http import UnreadablePostError

def skip_unreadable_post(record):
    if record.exc_info:
        exc_type, exc_value = record.exc_info[:2]
        if isinstance(exc_value, UnreadablePostError):
            return False
    return True

我想进一步过滤,但我需要知道此record参数中包含的内容,以便我可以操作它。

1 个答案:

答案 0 :(得分:0)

事实证明它是LogRecord。这可以通过系统地打印所述record属性来找到,如下所示:

def print_record(record):
    print '>>> %s' % type(record)
    return True


LOGGING = {
    [...]
    'filters': {
        'print_record': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': print_record,
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'filters': ['print_record'],
            'formatter': 'verbose',
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}
相关问题