本文介绍了使用dictConfig在python的日志级别上安装过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我无法使用dictConfig()语法在日志处理程序上安装筛选器. LoggingErrorFilter.filter()只是被忽略,什么也没有发生.

I cannot install a filter on a logging handler using dictConfig() syntax. LoggingErrorFilter.filter() is simply ignored, nothing happens.

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

I want to filter out error messages so that they do not appear twice in the log output. So I wrote LoggingErrorFilter class and overrode 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)

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

What am I doing wrong here? Why is my filter ignored?

推荐答案

实际上,Tupteq的答案通常是不正确的.以下脚本:

Actually, Tupteq's answer is not correct in general. The following script:

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')

运行时,将产生以下输出:

When run, produces the following output:

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

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

which shows that you can configure filters using dictConfig().

这篇关于使用dictConfig在python的日志级别上安装过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 16:52