【问题标题】:Python logger filter not work in msg %s variablesPython 记录器过滤器在 msg %s 变量中不起作用
【发布时间】:2020-09-04 01:05:48
【问题描述】:

您好,我在 python 日志记录中使用,我想使用过滤器替换控制台和调试文件中的密码。但它只在某些情况下有效

import logging
import logging.config
from collections import defaultdict

class AwesomeFilter(logging.Filter):
    def filter(self, rec):
        if 'password' in rec:
            return "here is password, i hide this line"
        else:
            return 1

LOG_CONFIG = {
    'version': 1,
    'filters': {
        'file_filter': {
            '()': AwesomeFilter,
        },
    },
    'disable_existing_loggers': True,


    'formatters': {
        'standard': {
            'format': '%(asctime)s %(name)s [%(levelname)s]: %(message)s'
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'INFO',
            'formatter': 'standard',
            'filters': ['file_filter'],
        },
    },
    'loggers': {
        '': {
            'handlers': ['file', 'console'],
            'level': 'DEBUG',
            'propagate': True
        },
    }
}

logging.config.dictConfig(LOG_CONFIG)

这行得通,如果我写了

log.info("line with my super password") - 在这里有效,替换整行

但是如果我想使用

log.info ("returned line is %s") and in %s is string "here is password" 过滤器忽略这一行,因为:

<LogRecord: PGSQLDatabaseConnection, 20, dluznici.py, 172, "returned line is: %s">

所以,这里没有“密码”这个词……如何检查 %s 中的密码?

也许我也必须在这里定义过滤器?

class PGSQLDatabase:
    def __init__(self, conn_params, session_params):
        self.log = logging.getLogger('PGSQLDatabaseConnection')
        self.filter = ????????????


【问题讨论】:

    标签: python python-3.x filter yaml


    【解决方案1】:

    好的,我可以在过滤器中搜索 args :)

    【讨论】:

      猜你喜欢
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2018-07-21
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多