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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to