Got some problems when using logging Filter

sword john.37 at gmail.com
Wed Nov 16 07:31:08 EST 2011


On Nov 16, 7:40 pm, Jean-Michel Pichavant <jeanmic... at sequans.com>
wrote:
> sword wrote:
> > The logging cookbook gives an Filter example, explainning how to add
> > contextural info to log. I can't figure out how to filter log from it.
>
> > Suppose I have 3 file, a.py, b.py and main.py
> > #file: a.py
> > import logging
>
> > logger=logging.getLogger(__name__)
> > def print_log():
> >     logger.debug("I'm module a")
>
> > #file: b.py just like a.py
> > import logging
> > logger=logging.getLogger(__name__)
> > def print_log():
> >     logger.debug("I'm module b")
>
> > #file: main.py
> > import logging
> > from logging import Filter
> > logging.basicConfig(level=logging.DEBUG)
> > logger=logging.getLogger("main")
> > logger.debug("This is main process")
> > logger.addFilter(Filter("a"))
>
> > And I expected that the console output would contain main and b module
> > log only. But it turned out that all logs there.  Is it the problem of
> > root logger?
>
> Hi,
>
> First of all, in the code you provided we can't see where you import a &
> b, and when you call their respective print_log method.
> Secondly,Filter("a") would allow only the "a" log events, not forbid
> them. quoting the docs: "if name is specified, it names a logger which,
> together with its children, will have its events allowed through the
> filter."
>
> As for your problem it may come from the fact that you applied the
> filter to the 'main' logger, while you probably want to add the filter
> to the *root* logger. Your current hierarchy is
>
> root
>   - main
>   - a
>   - b
>
> events fired from 'a' will be handled by the root logger, not the main.
> root = logging.getLogger()
> root.addFilter('main')
> root.addFilter('a')
> root.addFilter('b')
>
> JM

Thanks for your reply. I tried to edit the source a bit, now the
main.py looks like this:
#main.py
import logging
from logging import Filter
import a
import b

logging.basicConfig(level=logging.DEBUG)
root = logging.getLogger()
root.addFilter(Filter("GoneWithTheWind")) #so I suppose no log msg
would pass this filter

logger = logging.getLogger("main")
logger.debug("main process")
a.print_log()
b.print_log()

####
And It still prints out all the log msg. :(



More information about the Python-list mailing list