'logging' module - multiple messages

David N. Welton davidw at dedasys.com
Fri Feb 14 15:36:53 EST 2003


Robin Munn <rmunn at pobox.com> writes:

> David N. Welton <davidw at dedasys.com> wrote:

> > [ CC'ing replies to me appreciated - thanks. ]

> > It appears that it prints out a copy for each logger that exists:

> > WF|DEBUG|Logging enabled
> > WF.SC|DEBUG|Logging enabled
> > WF.SC|DEBUG|Logging enabled
> > WF|DEBUG|?|testing, testing, 1 2 3
> > WF.SC|DEBUG|?|prova, prova, 1 2 3
> > WF.SC|DEBUG|?|prova, prova, 1 2 3

> If you read the documentation for the logging module at

>     http://www.python.org/dev/doc/devel/lib/module-logging.html

Aha - I was using the red-dove site, which isn't as thorough.

[ ... ]

Yes, I had assumed it was something similar.

> So what's going on is that your "WF" logger is being considered the
> parent of "WF.SC" -- so when WF.SC logs a message, WF receives it
> too.  This will usually be what you want - you could then attach
> different handlers to the different elements in the tree (say WF.SC
> goes to the screen via a StreamHandler with level CRITICAL, and WF
> goes to a log file with level DEBUG, for example). If you really
> don't want that, you could create a Filter that checks the name of
> the logger that produced the message, like so:

>     # "Selfish" filter that doesn't listen to its children
>     class SelfishFilter(logging.Filter):
>         """Filter that doesn't allow messages from children"""
>         def filter(self, record):
>             return self.name == record.name

> Then you would attach this filter to your code like so:

>     class WFLog:
>         def __init__(self, name="WF"):
>             self.log = logging.getLogger(name)
>             f = logging.Formatter("%(name)s|%(levelname)s|%(message)s" )
>             h = logging.StreamHandler()
>             filter = SelfishFilter(name)
>             h.setFormatter(f)
>             self.log.addHandler(h)
>             self.log.addFilter(filter)
>             self.log.setLevel(logging.DEBUG)
>             self.log.debug("Logging enabled")

> That should give you the behavior you're looking for.

I still get two messages, but that's enough direction to go on to
figure things out.  Thanks!

-- 
David N. Welton
   Consulting: http://www.dedasys.com/
     Personal: http://www.dedasys.com/davidw/
Free Software: http://www.dedasys.com/freesoftware/
   Apache Tcl: http://tcl.apache.org/




More information about the Python-list mailing list