Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68393

Re: Clearing out handlers in logging?

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
To python-list@python.org
Subject Re: Clearing out handlers in logging?
In-Reply-To <gd.usenet-B1805D.09394016032014@dwarf.main.lan>
References <mailman.8159.1394938715.18130.python-list@python.org> <gd.usenet-B1805D.09394016032014@dwarf.main.lan>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8167.1394974663.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 2014-03-16 09:39, Gunther Dietrich wrote:
> Tim Chase <python.list@tim.thechases.com> 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





Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Clearing out handlers in logging? Tim Chase <python.list@tim.thechases.com> - 2014-03-15 21:58 -0500
  Re: Clearing out handlers in logging? Gunther Dietrich <gd.usenet@spamfence.net> - 2014-03-16 09:39 +0100
    Re: Clearing out handlers in logging? Chris Angelico <rosuav@gmail.com> - 2014-03-16 20:02 +1100
    Re: Clearing out handlers in logging? Peter Otten <__peter__@web.de> - 2014-03-16 10:18 +0100
    Re: Clearing out handlers in logging? Chris Angelico <rosuav@gmail.com> - 2014-03-16 20:35 +1100
    Re: Clearing out handlers in logging? Tim Chase <python.list@tim.thechases.com> - 2014-03-16 07:57 -0500
      Re: Clearing out handlers in logging? Gunther Dietrich <gd.usenet@spamfence.net> - 2014-03-16 19:29 +0100
        Re: Clearing out handlers in logging? Tim Chase <python.list@tim.thechases.com> - 2014-03-16 13:50 -0500

csiph-web