Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:134': 0.05; 'python': 0.08; '(it': 0.09; 'referenced': 0.09; 'def': 0.13; 'logger.': 0.16; 'over:': 0.16; 'references:': 0.16; 'roy': 0.16; 'subject:Overriding': 0.16; 'subject:global': 0.16; 'wrote:': 0.18; 'this?': 0.19; 'header:In-Reply-To:1': 0.22; 'module,': 0.23; 'code': 0.25; 'module': 0.26; 'function': 0.27; 'variable': 0.28; 'module.': 0.29; 'pm,': 0.29; 'class': 0.29; 'lines': 0.30; 'lot.': 0.30; 'pattern': 0.30; "i've": 0.31; 'skip:l 30': 0.32; 'implement': 0.32; 'header:User-Agent:1': 0.33; 'decide': 0.33; 'to:addr:python-list': 0.34; 'rest': 0.35; 'skip:" 10': 0.37; 'two': 0.37; 'but': 0.37; 'to:addr:python.org': 0.40; 'once': 0.60; 'more': 0.61; 'works,': 0.68; 'legal': 0.70; 'pardon': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmANAEYS506GuA9G/2dsb2JhbABEh2CgZoU/AQEFOFELGAklDwJGEwgCviCIU4MaBJR0hTmMcw Date: Tue, 13 Dec 2011 10:15:41 +0100 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20111110 Icedove/3.0.11 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Overriding a global References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323767811 news.xs4all.nl 6885 [2001:888:2000:d::a6]:47559 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17111 On 12/10/2011 09:47 PM, Roy Smith wrote: > I've got a code pattern I use a lot. In each module, I create a logger > for the entire module and log to it all over: > > logger = logging.getLogger('my.module.name') > > class Foo: > def function(self): > logger.debug('stuff') > logger.debug('other stuff') > > and so on. This works, but every once in a while I decide that a > particular function needs a more specific logger, so I can adjust the > logging level for that function independent of the rest of the module. > What I really want to do is: > > def function(self): > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? > How about two global references: globallogger = logger = logging.getLogger('my.module.name') def function(self): logger = globallogger.getChild('function') logger.debug('stuff') logger.debug('other stuff') -- Antoon Pardon