Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的日志异常过滤器_Python - Fatal编程技术网

python中的日志异常过滤器

python中的日志异常过滤器,python,Python,我想从日志记录到使用处理程序中排除一些不重要的特定异常 但是,我找不到任何方法来为这些处理程序配置过滤器 Java中类似的日志功能是 我想做这样的事情 import logging ... LOGGING = { 'loggers' = { 'django': { 'handlers': ['sentry', 'console'], 'filters': { 'exclude': [ObjectDoesNotExist,

我想从日志记录到使用处理程序中排除一些不重要的特定异常

但是,我找不到任何方法来为这些处理程序配置过滤器

Java中类似的日志功能是

我想做这样的事情

import logging
...

LOGGING = {
    'loggers' = {
       'django': {
        'handlers': ['sentry', 'console'],
        'filters': {
          'exclude': [ObjectDoesNotExist, ]
        },
        'level': 'ERROR',
       }
     }
}
logging
package是否支持此功能?如果没有,你介意告诉我实现这一目标的最佳方法吗


感谢Python没有内置的基于异常类型的日志过滤器,您必须编写一个:

class MyFilter(object):  
    def filter(self, record):
        if record.exc_info:
            etype, _, _ = record.exc_info
            for excluded_exc in [ObjectDoesNotExist,]:
                if issubclass(etype, excluded_exc):
                    return False
        return True
然后添加到日志配置中,注意特殊的“
()
”,这里是:


感谢@georgexsh的建议,我终于找到了这个解决方案

# config.log_filter.py
from logging import Filter
from django.core.exceptions import ObjectDoesNotExist

DEFAULT_EXCLUDE_EXCEPTIONS = [ObjectDoesNotExist, ]


class ExceptionFilter(Filter):
    def __init__(self, exclude_exceptions=DEFAULT_EXCLUDE_EXCEPTIONS, **kwargs):
      super(ExceptionFilter, self).__init__(**kwargs)
      self.EXCLUDE_EXCEPTIONS = exclude_exceptions

    def filter(self, record):
        if record.exc_info:
            etype, _, _ = record.exc_info
            for excluded_exception in self.EXCLUDE_EXCEPTIONS:
                if issubclass(etype, excluded_exception):
                    return False
        return True


# settings.common.py
...
LOGGING = {
    ...
    'filters': {
        'exception_filter': {
            '()': 'config.log_filter.ExceptionFilter'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'filters': ['exception_filter']
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
        ...
     }
}
logging.config.dictConfig(LOGGING)

希望它能帮助那些遇到同样问题的人

很高兴我能提供帮助这行代码的用途是什么:logging.config.dictConfig(logging)?@mogoh当你想用代码覆盖某些配置时,它是用于动态配置的-
# config.log_filter.py
from logging import Filter
from django.core.exceptions import ObjectDoesNotExist

DEFAULT_EXCLUDE_EXCEPTIONS = [ObjectDoesNotExist, ]


class ExceptionFilter(Filter):
    def __init__(self, exclude_exceptions=DEFAULT_EXCLUDE_EXCEPTIONS, **kwargs):
      super(ExceptionFilter, self).__init__(**kwargs)
      self.EXCLUDE_EXCEPTIONS = exclude_exceptions

    def filter(self, record):
        if record.exc_info:
            etype, _, _ = record.exc_info
            for excluded_exception in self.EXCLUDE_EXCEPTIONS:
                if issubclass(etype, excluded_exception):
                    return False
        return True


# settings.common.py
...
LOGGING = {
    ...
    'filters': {
        'exception_filter': {
            '()': 'config.log_filter.ExceptionFilter'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'filters': ['exception_filter']
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
        ...
     }
}
logging.config.dictConfig(LOGGING)