python logging 自定义 filter 配置

本文介绍了如何在Python logging模块中使用YML配置文件进行自定义filter配置。首先,展示了YML配置文件的关键部分,强调了自定义filter的key应为'()'而不是'class'。然后,详细解释了自定义配置读取函数的工作原理,通过将YML文件内容读取到dict并使用logging.config.dictConfig加载配置。读者可以借助文中提供的YML配置文件示例来理解这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、yml 配置文件配置

在这里插入图片描述如上图所示,注意用于指定自定义的 filter 类的 key 是 () 而不是 class,详情请看官方文档说明:官方文档说明,斜体部分内容摘自官方文档:
The schema supports user-defined objects for handlers, filters and formatters. (Loggers do not need to have different types for different instances, so there is no support in this configuration schema for user-defined logger classes.)
Objects to be configured are described by dictionaries which detail their configuration. In some places, the logging system will be able to infer from the context how an object is to be instantiated, but when a user-defined object is to be instantiated, the system will not know how to do this. In order to provide complete flexibility for user-defined object instantiation, the user needs to provide a ‘factory’ - a callable which is called with a configuration dictionary and which returns the instantiated object. This is signalled by an absolute import path to the factory being made available under the special key ‘()’.

2、自定义配置读取函数读取 yml 配置文件内容并加载
def load_logging():
    cur_dir = os.path.dirname(os.path.realpath(__file__))
    conf_file = os.path.join(cur_dir, 'logging.yml')
    with open(conf_file, "r") as f:
        config = yaml.load(f, yaml.SafeLoader)
    config['handlers']['info_file_handler']['filename'] = os.path.join(cur_dir, 'lego.log')
    logging.config.dictConfig(config)

如上面代码段所示,把 yml 配置文件的内容读取到一个 dict 类型的变量之后使用 lgging.config.dictConfig(存储yml文件内容的变量) 加载变量中存储的配置,如果配置项需要更改的话可以通过更改变量中的内容来实现。

附(yml 配置文件示例):

disable_existing_loggers: False
formatters:
        simple:
            format: "%(asctime)s - %(process)d - %(processName)s - %(thread)d - %(threadName)s - %(levelname)s - %(message)s"
filters:
    step_filter:
# https://docs.python.org/3.5/library/logging.config.html#logging-config-dict-connections
# () 指定对应的自定义类
            (): conf.logging_loader.StepsFilter
            name: root
handlers:
    console:
            class: logging.StreamHandler
            level: DEBUG
            formatter: simple
            stream: ext://sys.stdout
    info_file_handler:
            class: concurrent_log_handler.ConcurrentRotatingFileHandler
            level: INFO
            formatter: simple
            filename: lego.log
            maxBytes: 10485760
            backupCount: 20
            encoding: utf8
    database_handler:
            class: conf.logging_loader.DataBaseHandler
            level: INFO
            formatter: simple
            filters: [step_filter]
root:
    level: INFO
#    handlers: [console,info_file_handler,database_handler]
    handlers: [console,database_handler]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值