Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15767

Re: Got some problems when using logging Filter

Date 2011-11-16 12:40 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: Got some problems when using logging Filter
References <5b246f46-ca59-43a4-b9a6-29fdfc86822e@j19g2000pro.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2765.1321443604.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Got some problems when using logging Filter sword <john.37@gmail.com> - 2011-11-15 22:09 -0800
  Re: Got some problems when using logging Filter Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-11-16 12:40 +0100
    Re: Got some problems when using logging Filter sword <john.37@gmail.com> - 2011-11-16 04:31 -0800
      Re: Got some problems when using logging Filter Peter Otten <__peter__@web.de> - 2011-11-16 15:50 +0100
        Re: Got some problems when using logging Filter sword <john.37@gmail.com> - 2011-11-17 01:06 -0800
          Re: Got some problems when using logging Filter Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2011-11-19 16:42 -0800
            Re: Got some problems when using logging Filter sword <john.37@gmail.com> - 2011-11-20 23:15 -0800
              Re: Got some problems when using logging Filter Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2011-11-21 02:11 -0800
      Re: Got some problems when using logging Filter Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-11-21 12:39 +0100

csiph-web