1

I would like to change python logging file on the fly. I could do that by removing the existing handler and then adding a new one:

for hdlr in log.handlers:  # remove all old handlers
    log.removeHandler(hdlr)
log.addHandler(fileh) 

However, if my logger has multiple handlers, like stream handler, rotating file handler, , I would like to remove

2 Answers 2

3

Figured out the we could find the class of the file stream to remove selective handlers:

for handler in logger:
  if handler.__class__.__name__ == 'FileHandler':
    logger.removeHandler(handler)
Sign up to request clarification or add additional context in comments.

Comments

1

logger is not iterable. It has to be logger.handlers.

for handler in logger.handlers:
    if handler.__class__.__name__ == 'FileHandler':
    logger.removeHandler(handler)

Comments

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.