我无法使用dictConfig()
语法在日志处理程序上安装筛选器<代码>日志错误过滤器。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)
我做错了什么?为什么我的过滤器被忽略?
实际上,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
这表明您可以使用ukConfig()
配置过滤器。
您可以指定一个类名,但它是使用名奇怪的()
键完成的,并且必须包含模块名。例如。:
'filters': {
'errorfilter': {
'()' : '__main__.LoggingErrorFilter',
}
},
见16.7。2.4. 文档中的用户定义对象。
如果要从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运行记录器。记录器=日志记录。getlogger(“审计”)
运行应用程序