Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'handler': 0.05; 'one?': 0.05; 'root': 0.05; 'method.': 0.07; 'think,': 0.07; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'handler,': 0.16; 'handlers.': 0.16; 'logger': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'somewhere.': 0.16; 'subject:Clearing': 0.16; 'wrote:': 0.18; 'module': 0.19; 'written': 0.21; '>>>': 0.22; 'import': 0.22; 'hack': 0.22; 'install': 0.23; 'header:User-Agent:1': 0.23; 'sort': 0.25; 'logging': 0.26; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'tim': 0.29; "doesn't": 0.30; 'requests': 0.31; "skip:' 10": 0.31; 'too.': 0.31; '>>>>': 0.31; 'assert': 0.31; 'chase': 0.31; 'quite': 0.32; 'there,': 0.34; 'maybe': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'there': 0.35; 'doing': 0.36; 'method': 0.36; 'subject:?': 0.36; 'should': 0.36; 'e.g.': 0.38; 'tasks': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'remove': 0.60; 'read': 0.60; 'mentioned': 0.61; 'simple': 0.61; 'you.': 0.62; 'reach': 0.63; 'revealed': 0.68; 'dirty': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Clearing out handlers in logging? Date: Sun, 16 Mar 2014 10:18:15 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb79a.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394961511 news.xs4all.nl 2904 [2001:888:2000:d::a6]:41421 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68389 Gunther Dietrich wrote: > Tim Chase wrote: > >>The current (2.7; maybe 3.x?) logging module doesn't have any sort of >>"clear out all the current handlers" method. > > Indeed, THERE IS a removeHandler() method. In the documentation of > python 2.6, it is mentioned in '16.6.5. Logger Objects', directly after > the addHandler() method. If you found the addHandler() method there, you > should have found removeHandler() too. > > And a simple > >>>> dir(log) > ['__doc__', '__init__', '__module__', '_log', 'addFilter', 'addHandler', > 'callHandlers', 'critical', 'debug', 'disabled', 'error', 'exception', > 'fatal', 'filter', 'filters', 'findCaller', 'getEffectiveLevel', > 'handle', 'handlers', 'info', 'isEnabledFor', 'level', 'log', > 'makeRecord', 'manager', 'name', 'parent', 'propagate', 'removeFilter', > 'removeHandler', 'root', 'setLevel', 'warn', 'warning'] > > also would have revealed it to you. > > >>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. > > 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. > > >>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? > > One of the best ways would be to read the documentation. And to do some > exploration, e.g. by means of dir() and help(), could also be quite > instructive. This experience cannot be replaced by > documentation-research requests to the Usenet. Hm, what do the docs say about this one? >>> import logging >>> logging.basicConfig() >>> log = logging.getLogger("foo") >>> for i in range(5): ... log.addHandler(logging.FileHandler("tmp.log")) ... >>> assert len(log.handlers) == 5 >>> for handler in log.handlers: ... log.removeHandler(handler) ... >>> log.handlers [, ]