Clearing out handlers in logging?

Chris Angelico rosuav at gmail.com
Sun Mar 16 05:02:57 EDT 2014


On Sun, Mar 16, 2014 at 7:39 PM, Gunther Dietrich
<gd.usenet at spamfence.net> wrote:
>>but it feels a little dirty to reach into logging.root.handlers since
>>there are other methods for adding/removing handlers.  However, as
>>best I can tell, to remove a handler, you already need to have it
>>saved somewhere.
>
> What about this:
>
>>>> for handler in log.handlers:
> ...     log.removeHandler(handler)
>
> I think, that's just one of the tasks that removeHandler() was written
> for.

I'm sure Tim was aware of the removeHandler function. But this is
still reaching into log.handlers - although IMO it's safer to reach in
and read than to reach in and mutate. So without the implications of
Tim's inability to read docs, this is a viable suggestion. I'd prefer
this over the original "del log.handlers[:]".

ChrisA



More information about the Python-list mailing list