Search code examples
pythonlogginghandler

Updating handlers in QueueListener after it is created


is there anyway to change the number/config of handlers in the QueueListener after it is created?

I could recreate it every time I need, but that doesn't sound very efficient.


Solution

  • The handlers are accessible through a property called handlers. Simple example that shows how to remove the last handler:

    sh1 = logging.StreamHandler()
    sh2 = logging.StreamHandler()
    ql = logging.handlers.QueueListener(queue.SimpleQueue(), sh1, sh2)
    ql.handlers = ql.handlers[:-1]