4

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):
        <Some code>

1 Answer 1

8

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...

Source: Configuration dictionary schema.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That's something I did not know (obviously)
Glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.