Python日志记录:我可以将参数传递给json配置文件中的自定义处理程序吗 [英] Python logging: Can I pass arguments to a custom handler in json config file

查看:248
本文介绍了Python日志记录:我可以将参数传递给json配置文件中的自定义处理程序吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档,但是没有找到任何提及. 是否可以将参数传递到json配置文件中的自定义logging.handler类?

Ive read the docs, but did not find any mention of this. Is it possible to pass parameters to a custom logging.handler class inside a json configuration file?

"handlers": {
    "custom_handler": {
        "class": "logging.CustomHandler",
        "args": ['a', 'b'] # <------------------------            
        "level": "INFO",
        "formatter": "custom"
    }
},

处理程序类的定义是:

class CustomHandler(logging.Handler):
    def __init__(self, argA, argB):
        super().__init__()
        self.a = argA
        self.b = argB
    def emit(self, record):
        <Some code>

推荐答案

处理程序部分中不是classlevelformatterfilters之一的每个键都作为关键字传递给处理程序构造函数争论.示例:

Every key in the handler section that is not one of class, level, formatter or filters is passed to handler constructor as keyword argument. Example:

"handlers": {
    "custom_handler": {
        "class": "logging.CustomHandler",
        "level": "INFO",
        "formatter": "custom",
        "argA": "spam",
        "argB": "eggs"
    }
}

这也意味着拥有一个带有名为classlevelformatterfilters的构造函数arg的处理程序是一个坏主意...

This also means that having a handler with a constructor arg named class, level, formatter or filters is a bad idea...

来源:配置字典架构

这篇关于Python日志记录:我可以将参数传递给json配置文件中的自定义处理程序吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆