Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'source,': 0.04; 'handler': 0.05; 'method.': 0.07; 'modified': 0.07; 'modifying': 0.07; 'think,': 0.07; '"if': 0.09; 'indeed,': 0.09; 'iterate': 0.09; 'messing': 0.09; 'override': 0.09; 'def': 0.12; '2.7': 0.14; 'mostly': 0.14; '*should*': 0.16; '-tkc': 0.16; '>the': 0.16; 'discarded': 0.16; 'docs.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'handlers.': 0.16; 'naming': 0.16; 'publicly,': 0.16; 'subject:Clearing': 0.16; 'undocumented': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'module': 0.19; 'written': 0.21; '>>>': 0.22; "aren't": 0.24; "shouldn't": 0.24; 'question': 0.24; 'sort': 0.25; 'source': 0.25; 'logging': 0.26; 'possibly': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'appear': 0.29; 'chris': 0.29; 'tim': 0.29; "doesn't": 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; '(unless': 0.31; 'chase': 0.31; 'publicly': 0.31; 'skip:r 60': 0.31; 'maybe': 0.34; "i'd": 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'interface,': 0.36; 'yield': 0.36; 'doing': 0.36; 'charset:us- ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'basis.': 0.38; 'stable': 0.38; 'tasks': 0.38; 'to:addr:python- list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'reaching': 0.61; "you're": 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'default': 0.69; 'find.': 0.84; 'received:50.22': 0.84; 'hands': 0.96 Date: Sun, 16 Mar 2014 07:57:59 -0500 From: Tim Chase To: python-list@python.org Subject: Re: Clearing out handlers in logging? In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com 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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394974663 news.xs4all.nl 2877 [2001:888:2000:d::a6]:34149 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68393 On 2014-03-16 09:39, 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. Yes, I'm aware of it, having read the source & docs. However, the signature is removeHandler(some_handle_to_a_handler_that_you_already_have) and at the point in my code where I need to override this, basicConfig() has already assigned a default stream handler and discarded the handle/name/reference to it because the .debug/.info/.warn/.error/.critical methods of all call basicConfig() if there aren't any handlers. [Aside: which is non-pythonically, using "if len(self.handlers) == 0" instead of "if not self.handlers" in addition to the camel-case naming (which would be much harder to fix)] > >>> for handler in log.handlers: > ... log.removeHandler(handler) > > I think, that's just one of the tasks that removeHandler() was > written for. The question revolves mostly around dipping your hands into the undocumented .handlers property. I can nuke it directly (and looking at least at the 2.7 source, there doesn't seem to be much issue with doing this), or I can iterate (as modified by Chris Angelico to prevent modifying the list over which you're iterating). Either way, it still involves reaching into log.handlers which doesn't appear in any of the documentation as far as I could find. So yes, I use help() and dir() on a daily basis. But just because something is there doesn't mean I should go mucking with it in the event it's undocumented. If it *should* be trusted as a publicly available interface, it should be documented; if it shouldn't be treated publicly, then it should have a way to iterate over them such as def iterHandlers(self): for h in self.handlers[:]: yield h so you can do for h in log.iterHandlers(): log.removeHandler(h) Given how stable this code has been over the years, I'd just document the log.handlers property and possibly advise to lock around messing with it (unless "del log.handlers[:]" is atomic). -tkc