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"
}
},
Where the handler class definition is :
class CustomHandler(logging.Handler):
def __init__(self, argA, argB):
super().__init__()
self.a = argA
self.b = argB
def emit(self, record):
解决方案
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"
}
}
This also means that having a handler with a constructor arg named class, level, formatter or filters is a bad idea...