Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!nuzba.szn.dk!pnx.dk!amsnews11.chello.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"if': 0.04; 'subject:using': 0.04; 'suppose': 0.05; 'subject:when': 0.07; '"a"': 0.09; 'specified,': 0.09; 'subject:problems': 0.09; 'output': 0.10; 'def': 0.14; 'method.': 0.15; 'cc:addr:python- list': 0.15; "'a'": 0.16; 'a.py': 0.16; 'b.py': 0.16; 'cookbook': 0.16; 'hierarchy': 0.16; 'logger.': 0.16; 'received:192.168.200': 0.16; 'subject:Filter': 0.16; 'subject:Got': 0.16; 'subject:logging': 0.16; 'wrote:': 0.18; 'figure': 0.20; 'cc:no real name:2**0': 0.21; 'file,': 0.21; 'log.': 0.23; 'header:In- Reply-To:1': 0.23; 'there.': 0.24; 'cc:2**0': 0.25; 'code': 0.25; 'fact': 0.27; 'all,': 0.27; 'import': 0.28; 'cc:addr:python.org': 0.29; 'problem': 0.29; 'module': 0.30; 'turned': 0.30; 'quoting': 0.30; 'subject:some': 0.30; 'hi,': 0.31; 'skip:l 30': 0.32; "can't": 0.32; 'it.': 0.33; 'header:User-Agent:1': 0.33; 'filter': 0.34; 'handled': 0.34; 'probably': 0.34; 'root': 0.34; 'which,': 0.34; 'example,': 0.36; 'but': 0.37; 'received:192': 0.38; 'logs': 0.39; 'subject:: ': 0.39; 'only.': 0.40; 'skip:l 20': 0.40; 'header:Received:6': 0.60; 'your': 0.61; 'provided': 0.61; "'main'": 0.84; 'forbid': 0.84; 'fired': 0.91 X-IronPort-AV: E=Sophos;i="4.69,520,1315173600"; d="scan'208";a="2398809" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Wed, 16 Nov 2011 12:40:02 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: sword Subject: Re: Got some problems when using logging Filter References: <5b246f46-ca59-43a4-b9a6-29fdfc86822e@j19g2000pro.googlegroups.com> In-Reply-To: <5b246f46-ca59-43a4-b9a6-29fdfc86822e@j19g2000pro.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321443604 news.xs4all.nl 6987 [2001:888:2000:d::a6]:58486 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15767 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