使用dictConfig在python中的日志记录级别安装过滤器

时间:2014-01-30 11:41:27

标签: python python-2.7 logging

我无法使用dictConfig()语法在日志记录处理程序上安装过滤器。 LoggingErrorFilter.filter()被忽略,没有任何反应。

我想过滤掉错误消息,以便它们不会在日志输出中出现两次。所以我写了LoggingErrorFilter课程并覆盖了filter()

我的配置:

class LoggingErrorFilter(logging.Filter):
  def filter(self, record):
    print 'filter!'
    return record.levelno == logging.ERROR or record.levelno == logging.CRITICAL

config = {
      'version': 1,
      'disable_existing_loggers' : False,
      'formatters' : {
        'standard' : {
          'format' : '%(asctime)s %(levelname)s %(name)s::%(message)s',
        },
      },
      'handlers' : {
        'console': {
          'class' : 'logging.StreamHandler',
          'level' : level,
          'formatter' : 'standard',
          'stream' : 'ext://sys.stdout',
        },
        'errorconsole': {
          'class' : 'logging.StreamHandler',
          'level' : 'ERROR',
          'formatter' : 'standard',
          'stream' : 'ext://sys.stderr',
          'filters'  :['errorfilter',],
        },
      },
      'filters': {
        'errorfilter': {
          'class' : 'LoggingErrorFilter',
        }
      },
      'loggers' : {
        '' : {
          'handlers' : ['errorconsole','console',],
          'level' : level,
          'propagate' : True,
        },
        name : {
          'handlers' : ['errorconsole','console',],
          'level' : level,
          'propagate' : False,
        },
      },
  }
  logging.config.dictConfig(config)

我在这里做错了什么?为什么忽略我的过滤器?

3 个答案:

答案 0 :(得分:27)

实际上,Tupteq的回答一般不正确。以下脚本:

import logging
import logging.config
import sys

class MyFilter(logging.Filter):
    def __init__(self, param=None):
        self.param = param

    def filter(self, record):
        if self.param is None:
            allow = True
        else:
            allow = self.param not in record.msg
        if allow:
            record.msg = 'changed: ' + record.msg
        return allow

LOGGING = {
    'version': 1,
    'filters': {
        'myfilter': {
            '()': MyFilter,
            'param': 'noshow',
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'filters': ['myfilter']
        }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['console']
    },
}

if __name__ == '__main__':
    print(sys.version)
    logging.config.dictConfig(LOGGING)
    logging.debug('hello')
    logging.debug('hello - noshow')

运行时,产生以下输出:

$ python filtcfg.py 
2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1]
changed: hello

表明您可以使用dictConfig()配置过滤器。

答案 1 :(得分:7)

您可以指定一个类名,但它使用奇怪命名的()键完成,并且必须包含模块名称。 E.g:

 'filters': {
    'errorfilter': {
      '()' : '__main__.LoggingErrorFilter',
    }
  },

请参阅文档中的16.7.2.4. User-defined objects

答案 2 :(得分:0)

如果要从json文件加载。这应该是这样

{
  "version": 1,
  "grey": "/x1b[38;21m",
  "yellow": "/x1b[33;21m",
  "red": "/x1b[31;21m",
  "bold_red": "/x1b[31;1m",
  "reset": "/x1b[0m",
  "filters": {
    "audit_filter": {
      "()": "app.logs.AuditFilter.AuditFilter",
      "param": "audit"
    },
    "ignore_audit_filter": {
      "()": "app.logs.AuditFilter.IgnoreAuditFilter",
      "param": "audit"
    }
  },
  "formatters": {
    "package_formatter": {
      "format": "[%(asctime)s] - [%(levelname)s] - [%(name)s] : %(message)s"
    }
  },
  "handlers": {
    "console": {
      "class": "logging.StreamHandler",
      "level": "DEBUG",
      "formatter": "package_formatter"
    },
    "development_file_handler": {
      "class": "logging.FileHandler",
      "filename": "../debug.log",
      "level": "DEBUG",
      "formatter": "package_formatter",
      "filters": [
        "ignore_audit_filter"
      ]
    },
    "audit_file_handler": {
      "class": "logging.FileHandler",
      "filename": "../audit.log",
      "level": "DEBUG",
      "formatter": "package_formatter",
      "filters": [
        "audit_filter"
      ]
    }
  },
  "loggers": {
    "audit": {
      "handlers": [

        "audit_file_handler"
      ],
      "level": "INFO"
    }
  },
  "root": {
    "level": "INFO",
    "handlers": [
      "console",
      "development_file_handler"
    ]
  }
}

初始化然后附加记录器并运行记录器 使用app.logger = logging.getlogger("audit")

运行应用程序