Trying to understand logging.Filter

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Wed Jan 25 07:14:05 EST 2006


I'd expect the program below to put only one line with "Eat me" in the log
file. However, I get this with Python 2.4.2 in WingIDE on Windows XP:

  Eat me
  Ignore me

My understanding is:
1. Event "Ignore me" is logged to logger 'spam'.
2. Logger 'spam' has no handler associated with it, so this logger doesn't
do anything with the event.
3. The event is dispatched to the root logger as well, since this is an
ancestor of 'spam'. This works as expected, since the event "Eat me" is
logged by the root logger.
4. The root logger has a Filter, which requires the logger of the event to
be 'ham', or offspring thereof. 'spam' is not offspring of 'ham' in the
dotted name hierarchy, so the event should be ignored.

So why does "Ignore me" end up in the logfile? What am I missing?

import logging

rootLogger = logging.getLogger()
rootLogger.addHandler(logging.FileHandler(r'C:\Scratch\SiteChecker\Sitechecker.log'))
rootLogger.addFilter(logging.Filter('ham'))
rootLogger.setLevel(logging.DEBUG)

logging.getLogger('ham.eggs').info("Eat me")
logging.getLogger('spam').info("Ignore me")
logging.getLogger().info("Ignore me too")

logging.shutdown()

-- 
René Pijlman

Wat wil jij worden?  http://www.carrieretijger.nl



More information about the Python-list mailing list