471,578 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,578 software developers and data experts.

Trying to understand logging.Filter

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:\Scr atch\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
Jan 25 '06 #1
1 3001
Rene Pijlman:
3. The event is dispatched to the root logger as well,


Got it. The event is not dispatched to ancestor loggers, but to the
handlers associated with ancestor loggers (Doc: "In addition to any
handlers directly associated with a logger, all handlers associated with
all ancestors of the logger are called to dispatch the message.").

Filters of ancester loggers are not applied, therefore, but filters of
handlers of ancestor loggers are. When I add the filter to the handler
rather than the logger, it works as expected:

import logging

rootLogger = logging.getLogger()
handler = logging.FileHandler(r'C:\Scratch\SiteChecker\Sitec hecker.log')
handler.addFilter(logging.Filter('ham'))
rootLogger.addHandler(handler)
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
Jan 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by chuck | last post: by
5 posts views Thread by Ritesh Raj Sarraf | last post: by
3 posts views Thread by Gary Jefferson | last post: by
12 posts views Thread by Eric S. Johansson | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.