Clearing out handlers in logging?

Tim Chase python.list at tim.thechases.com
Sat Mar 15 22:58:45 EDT 2014


The current (2.7; maybe 3.x?) logging module doesn't have any sort of
"clear out all the current handlers" method.  I can hack it by doing

  log = logging.getLogger()  # get the root logger
  del log.handlers[:]        # reach inside and nuke 'em
  log.addHandler(...)        # install the one(s) I want

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.

Is there a better way to do this, or do I just suck it up and deal
with the (potentially thread-ignorant, as it doesn't lock) hack?

Thanks,

-tkc









More information about the Python-list mailing list