473,508 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

selective logger disable/enable

Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B',
and each module has its own logger, created with:

module1logger = logging.getLogger('project.A')

and

module2logger = logging.getLogger('project.A.a')

and

module3logger = logging.getLogger('project.B')
And I want to selectively enable/disable these, per module (for
example, I only want logging from A and A.a, or just from B, etc). It
seems like logging.Filter is supposed to let me do this, but I can't
see how to apply it globally. Is there really no other way that to add
a addFilter(filter) call to each of these loggers individually?

logging.basicConfig gets inherited by all loggers, but it doesn't seem
capable of giving a Filter to all loggers. Is there any way to do this?

Jan 19 '07 #1
10 8060
Gary Jefferson wrote:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B',
and each module has its own logger, created with:

module1logger = logging.getLogger('project.A')

and

module2logger = logging.getLogger('project.A.a')

and

module3logger = logging.getLogger('project.B')
And I want to selectively enable/disable these, per module (for
example, I only want logging from A and A.a, or just from B, etc). It
seems like logging.Filter is supposed to let me do this, but I can't
see how to apply it globally. Is there really no other way that to add
a addFilter(filter) call to each of these loggers individually?
The documentation for Logger - see

http://docs.python.org/lib/node406.html

- shows that there are addFilter() and removeFilter() methods on the
Logger class which you can use to add or remove filters from individual
Logger instances. From the above page (entitled "14.5.1 Logger
Objects"):

addFilter(filt)
Adds the specified filter filt to this logger.

removeFilter(filt)
Removes the specified filter filt from this logger.

The parent section of Section 14.5.1, which is Section 14.5, was the
first search result when I just searched Google for "python logging".

Best regards,

Vinay Sajip

Jan 19 '07 #2

Vinay Sajip wrote:
>
The documentation for Logger - see

http://docs.python.org/lib/node406.html

- shows that there are addFilter() and removeFilter() methods on the
Logger class which you can use to add or remove filters from individual
Logger instances. From the above page (entitled "14.5.1 Logger
Objects"):

addFilter(filt)
Adds the specified filter filt to this logger.

removeFilter(filt)
Removes the specified filter filt from this logger.

The parent section of Section 14.5.1, which is Section 14.5, was the
first search result when I just searched Google for "python logging".

Thanks for the reply Vinay. I had been reading those docs prior to
posting, but addFilter/removeFilter only work on individual logger
instances and handlers. I want to globally enable/disable multiple
logger instances, based on their namespaces (see the example I
provided). I cannot find a way to do this. I can certainly call
addFilter on every instance of every logger throughout the source code,
but this is more than inconvenient -- it breaks when new modules are
added with their own loggers in their own namespaces (until fixed by
manually adding addFilter() to those instances).

e.g., suppose you have a source tree with a several components that do
network access, and several components that do other things like saving
state to disk, accessing the filesystem, etc. And suppose you find a
bug that you think is in the networking code. It would be nice to be
able to GLOBALLY enable just the loggers that belong in the networking
namespace, and disable all others (similar to how Filters are supposed
to work for individual loggers). And then to be able to do this with
any component, by providing, for example, a command line switch or
environment variable. Otherwise, the poor programmer is forced to go
and edit every module in the source tree to selectively turn on/off
their respecitve loggers. Or am I missing something really obvious
about how this is done with the logging module?

thanks,
Gary

Jan 21 '07 #3
Gary Jefferson wrote:
Thanks for the reply Vinay. I had been reading those docs prior to
posting, but addFilter/removeFilter only work on individual logger
instances and handlers. I want to globally enable/disable multiple
logger instances, based on their namespaces (see the example I
provided). I cannot find a way to do this. I can certainly call
addFilter on every instance of every logger throughout the source code,
but this is more than inconvenient -- it breaks when new modules are
added with their own loggers in their own namespaces (until fixed by
manually adding addFilter() to those instances).

e.g., suppose you have a source tree with a several components that do
network access, and several components that do other things like saving
state to disk, accessing the filesystem, etc. And suppose you find a
bug that you think is in the networking code. It would be nice to be
able to GLOBALLY enable just the loggers that belong in the networking
namespace, and disable all others (similar to how Filters are supposed
to work for individual loggers). And then to be able to do this with
any component, by providing, for example, a command line switch or
environment variable. Otherwise, the poor programmer is forced to go
and edit every module in the source tree to selectively turn on/off
their respecitve loggers. Or am I missing something really obvious
about how this is done with the logging module?
I don't know enough about your target environment and application to
necessarily give you the best advice, but I'll make some general
comments which I hope are useful. You seem to be thinking that loggers
are binary - i.e. you turn them on or off. But they can be controlled
more finely than that; you should be able to get the effect that you
want by using the different logging levels available judiciously, as
well as using the fact that loggers inhabit a named hierarchy. Note
that loggers, by default, inherit the level of the first ancestor
logger which has an explicitly set level (by ancestor, I mean in the
name hierarchy). The root logger's default level is WARNING, so by
default all loggers will work at this level. [N.B. You can also set
levels for individual handlers, e.g. to ensure that only CRITICAL
conditions are emailed to a specified email address using SMTPHandler,
or that ERROR conditions and above are written to file but not to
console.]

So, for your networking scenario, let's suppose you do the following:
Have all network loggers live in the hierarchy namespace below
"network" (e.g. "network", "network.tcp", "network.http" etc.). By
default, all of these will only log events of severity WARNING and
above. Also, suppose you log events in your application code, which are
sometimes but not always of interest, at level DEBUG or level INFO.
Then, these events will never show up in the logging output, since they
are below WARNING in severity. Subsequently, if you want to turn on
logging verbosity for the network code only, you can arrange, via a
command-line switch or environment variable or configuration file, to
do

logging.getLogger("network").setLevel(logging.DEBU G)

whereupon you will start seeing events from the networking code at
severity DEBUG and INFO. This will affect all loggers in the "network"
hierarchy whose levels you have not explicitly set (so that they will
get the effective level of the first ancestor which has a level
explicitly set - the logger named "network").

If all you are interested in is turning on verbosity based on different
event severities (levels), you should not need to use or set Filters.
Filters are for use only when levels don't meet your use case
requirements.

Best regards,

Vinay Sajip

Jan 21 '07 #4

Vinay Sajip wrote:
>
I don't know enough about your target environment and application to
necessarily give you the best advice, but I'll make some general
comments which I hope are useful. You seem to be thinking that loggers
are binary - i.e. you turn them on or off. But they can be controlled
more finely than that; you should be able to get the effect that you
want by using the different logging levels available judiciously, as
well as using the fact that loggers inhabit a named hierarchy. Note
that loggers, by default, inherit the level of the first ancestor
logger which has an explicitly set level (by ancestor, I mean in the
name hierarchy). The root logger's default level is WARNING, so by
default all loggers will work at this level. [N.B. You can also set
levels for individual handlers, e.g. to ensure that only CRITICAL
conditions are emailed to a specified email address using SMTPHandler,
or that ERROR conditions and above are written to file but not to
console.]

So, for your networking scenario, let's suppose you do the following:
Have all network loggers live in the hierarchy namespace below
"network" (e.g. "network", "network.tcp", "network.http" etc.). By
default, all of these will only log events of severity WARNING and
above. Also, suppose you log events in your application code, which are
sometimes but not always of interest, at level DEBUG or level INFO.
Then, these events will never show up in the logging output, since they
are below WARNING in severity. Subsequently, if you want to turn on
logging verbosity for the network code only, you can arrange, via a
command-line switch or environment variable or configuration file, to
do

logging.getLogger("network").setLevel(logging.DEBU G)

whereupon you will start seeing events from the networking code at
severity DEBUG and INFO. This will affect all loggers in the "network"
hierarchy whose levels you have not explicitly set (so that they will
get the effective level of the first ancestor which has a level
explicitly set - the logger named "network").

If all you are interested in is turning on verbosity based on different
event severities (levels), you should not need to use or set Filters.
Filters are for use only when levels don't meet your use case
requirements.

Best regards,

Vinay Sajip

Vinay, okay, I think what you described will work out for me -- thank
you very much for the explanation.

I am still a bit confused about Filters, though. It seems they are a
bit of an anomoly in the hierarchical view of loggers that the API
supports elsewhere, i.e., filters don't seem to inherit... Or am I
missing something again? Here's a quick example:

import logging

log1 = logging.getLogger("top")
log2 = logging.getLogger("top.network")
log3 = logging.getLogger("top.network.tcp")
log4 = logging.getLogger("top.network.http")
log5 = logging.getLogger("top.config")
log6 = logging.getLogger("top.config.file")

logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s')

filter = logging.Filter("top.network")
log1.addFilter(filter) # only affects log1, do this for each of log2-7
too?

log1.debug("I'm top")
log2.debug("I'm top.network")
log3.debug("I'm top.network.tcp")
log4.debug("I'm top.network.http")
log5.debug("I'm top.config")
log6.debug("I'm top.config.file")
This is only for the binary case (and I think if I ignore the binary
case and filters altogether as you suggested), but it really would be
nice to be able to squelch /all/ output from loggers that belong to
certain parts of the namespace by using a filter as above (which I
can't get to work).

Perhaps if I set up a basicConfig with a loglevel of nothing, I can get
this to approximate squelching of everything but that which I
explicitly setLevel (which does inherit properly).

In other words, the addFilter/removeFilter part of the API seems rather
impotent if it can't be inherited in the logging namespaces. In fact,
I can't really figure out a use case where I could possibly want to use
it without it inheriting. Obviously I'm missing something. I'm sure
I've consumed more attention that I deserve already in this thread,
but, do you have any pointers which can enlighten me as to how to
effectively use addFilter/removeFilter?

many thanks,
Gary

Jan 22 '07 #5
Gary Jefferson wrote:
I am still a bit confused about Filters, though. It seems they are a
bit of an anomoly in the hierarchical view of loggers that the API
supports elsewhere, i.e., filters don't seem to inherit... Or am I
missing something again? Here's a quick example:

import logging

log1 = logging.getLogger("top")
log2 = logging.getLogger("top.network")
log3 = logging.getLogger("top.network.tcp")
log4 = logging.getLogger("top.network.http")
log5 = logging.getLogger("top.config")
log6 = logging.getLogger("top.config.file")

logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s')

filter = logging.Filter("top.network")
log1.addFilter(filter) # only affects log1, do this for each of log2-7
too?

log1.debug("I'm top")
log2.debug("I'm top.network")
log3.debug("I'm top.network.tcp")
log4.debug("I'm top.network.http")
log5.debug("I'm top.config")
log6.debug("I'm top.config.file")
This is only for the binary case (and I think if I ignore the binary
case and filters altogether as you suggested), but it really would be
nice to be able to squelch /all/ output from loggers that belong to
certain parts of the namespace by using a filter as above (which I
can't get to work).

Perhaps if I set up a basicConfig with a loglevel of nothing, I can get
this to approximate squelching of everything but that which I
explicitly setLevel (which does inherit properly).

In other words, the addFilter/removeFilter part of the API seems rather
impotent if it can't be inherited in the logging namespaces. In fact,
I can't really figure out a use case where I could possibly want to use
it without it inheriting. Obviously I'm missing something. I'm sure
I've consumed more attention that I deserve already in this thread,
but, do you have any pointers which can enlighten me as to how to
effectively use addFilter/removeFilter?
I don't really think there's a problem with the logging API - it went
through a fair amount of peer review on python-dev before making it
into the Python distribution. You need to use what's there rather than
shoehorn it into how you think it ought to be. Filters are for more
esoteric requirements - you can see some examples of filters in the old
(out of date) standalone distribution at
http://www.red-dove.com/python_logging.html (download and examine some
of the test scripts). Loggers aren't binary - levels are there to be
used. Most people get by with judicious use of levels, using Filters on
loggers only for unusual cases. Since Filters are only meant to be used
in unusual situations, there is no need to think about inheriting them.

Filters can be set on Handlers so that even if Loggers log the events,
they don't go to any output if filtered out at the handlers for that
output.

BTW I would also advise reading PEP-282 to understand more about the
logging approach.

Best regards,

Vinay Sajip

Jan 22 '07 #6

Vinay Sajip wrote:
>
BTW I would also advise reading PEP-282 to understand more about the
logging approach.

You've been most helpful, Vinay. The PEP section on Filters states
that I can do what I've been trying to do with filters, but doesn't
provide enough information to do it (or, at least, I'm too thick to
guess at how it would work by looking at the API and PEP and trying a
dozen different ways). Luckily, the examples you point to from your
original package do provide enough info; log_test15.py held the key.

I still feel like it would be more intuitive if filters were inherited
down the hierarchy instead of having to go through the extra steps of
getting at the root handler first, but I'm sure there are good reasons
for not doing this.

One more question, is there any way to get the list of all named
loggers (from the Manager, perhaps)? Or... maybe I don't need this,
either, as MatchFilter (log_test18.py) seems to do what I was thinking
I need the list of logger names for... most excellent.

Thanks,
Gary

BTW, the python logging module is one of the best readily available
loggers I've come across in any language.

Jan 23 '07 #7
Gary Jefferson wrote:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B',
and each module has its own logger, created with:

module1logger = logging.getLogger('project.A')

and

module2logger = logging.getLogger('project.A.a')

and

module3logger = logging.getLogger('project.B')
And I want to selectively enable/disable these, per module (for
example, I only want logging from A and A.a, or just from B, etc). It
seems like logging.Filter is supposed to let me do this, but I can't
see how to apply it globally. Is there really no other way that to add
a addFilter(filter) call to each of these loggers individually?

logging.basicConfig gets inherited by all loggers, but it doesn't seem
capable of giving a Filter to all loggers. Is there any way to do this?
An alternative approach might be to set the 'propagate' flag:

import logging

def warn_all(loggers, message):
for lgr in loggers:
lgr.warn(message)

logging.basicConfig()
root = logging.getLogger()
root.manager.emittedNoHandlerWarning = True
loggers = [logging.getLogger(name) for name in """
alpha
alpha.1
alpha.2
alpha.2.1
alpha.2.2
alpha.2.2.1
beta
beta.1
beta.2
""".split()]

warn_all(loggers, "all on")

print "---"
logging.getLogger("alpha").propagate = False
warn_all(loggers, "alpha off")
logging.getLogger("alpha").propagate = True

print "---"
logging.getLogger("alpha.2").propagate = False
warn_all(loggers, "alpha.2 off")

Peter
Jan 23 '07 #8
Gary Jefferson wrote:
Vinay Sajip wrote:

BTW I would also advise reading PEP-282 to understand more about the
logging approach.


You've been most helpful, Vinay. The PEP section on Filters states
that I can do what I've been trying to do with filters, but doesn't
provide enough information to do it (or, at least, I'm too thick to
guess at how it would work by looking at the API and PEP and trying a
dozen different ways). Luckily, the examples you point to from your
original package do provide enough info; log_test15.py held the key.

I still feel like it would be more intuitive if filters were inherited
down the hierarchy instead of having to go through the extra steps of
getting at the root handler first, but I'm sure there are good reasons
for not doing this.

One more question, is there any way to get the list of all named
loggers (from the Manager, perhaps)? Or... maybe I don't need this,
either, as MatchFilter (log_test18.py) seems to do what I was thinking
I need the list of logger names for... most excellent.

Thanks,
Gary

BTW, the python logging module is one of the best readily available
loggers I've come across in any language.
Thanks. Glad the tests/examples (log_testxx.py) helped. When I get a
chance, I will try to work some of them into the docs...

Best regards,
Vinay Sajip

Jan 23 '07 #9
So maybe I don't have all this figured out quite as well as I thought.
What I really want to do is set an environment variable, MYDEBUG, which
contains a list of wildcarded logger names, such as "a.*.c a.d" (which
becomes ['a.*.c', 'a.d'], and then selectively crank the loglevel up to
DEBUG for those that match.

In order to do that, I think I need something kind of like filters, but
for logger name... I'm not seeing how to do this, even after playing
with variations of test15, 18, and 21.

What I do have working at the moment is passing a list of non-wildcard
logger names, i.e., doing the wildcard expansion manually such as
"['a.b.c', 'a.c.c', 'a.f.c', 'a.d']. Is there anyway to automate this
dynamically?

BTW, I do understand that 'a.d' is essentially equivalent to 'a.d.*',
but I'm dealing with a hierarchy that doesn't always tidy up like that.
For example, I have top.network.server.http, top.network.server.smtp,
top.network.client.http, and top.network.client.smtp. Obviously, if I
only want server or client DEBUG msgs, this is easy. And sometimes
that's exactly what I want. Other times, I want only smtp DEBUG msgs,
and the hierarchy won't help me get that (unless I break it for just
getting client or server msgs), etc. So I would really like to figure
out how to do 'a.*.c'.

Any ideas?

Thanks again,
Gary
On Jan 23, 3:01 am, "Vinay Sajip" <vinay_sa...@yahoo.co.ukwrote:
Glad the tests/examples (log_testxx.py) helped. When I get a
chance, I will try to work some of them into the docs...

Best regards,

Vinay Sajip
Jan 25 '07 #10
Gary Jefferson wrote:
So maybe I don't have all this figured out quite as well as I thought.
What I really want to do is set an environment variable, MYDEBUG, which
contains a list of wildcarded logger names, such as "a.*.c a.d" (which
becomes ['a.*.c', 'a.d'], and then selectively crank the loglevel up to
DEBUG for those that match.

In order to do that, I think I need something kind of like filters, but
for logger name... I'm not seeing how to do this, even after playing
with variations of test15, 18, and 21.

What I do have working at the moment is passing a list of non-wildcard
logger names, i.e., doing the wildcard expansion manually such as
"['a.b.c', 'a.c.c', 'a.f.c', 'a.d']. Is there anyway to automate this
dynamically?

BTW, I do understand that 'a.d' is essentially equivalent to 'a.d.*',
but I'm dealing with a hierarchy that doesn't always tidy up like that.
For example, I have top.network.server.http, top.network.server.smtp,
top.network.client.http, and top.network.client.smtp. Obviously, if I
only want server or client DEBUG msgs, this is easy. And sometimes
that's exactly what I want. Other times, I want only smtp DEBUG msgs,
and the hierarchy won't help me get that (unless I break it for just
getting client or server msgs), etc. So I would really like to figure
out how to do 'a.*.c'.

Any ideas?

Thanks again,
Gary

Okay, I think I'm back on track again: I can do regex comparisons
against the name field of the LogRecord instance handed to me by
filter().

Gary

Jan 25 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15936
by: Alphonse Giambrone | last post by:
I am trying to enable/disable a requiredfieldvalidator on the client side and am generating an error. I had found some documentation on validation which states that I should be able to...
0
3351
by: Robert Ladd | last post by:
Hi, I'm trying to disable the asp.net calendar control from a javascript function, but it doesn't disable the doPostBack. To simplify the situation, assume a page with 4 controls. A...
0
2383
by: talal | last post by:
How to check whether javascript is enable or disable in client browser. Remeber i can check wheter browser client support javascript with BrowserCapability class. But what if the client has disable...
4
10728
by: mabond | last post by:
Does anyone know some code that will allow me to enable and/or disable a task which already exists in the task scheduer. I have a task which runs at 15 min intervals and from time to time I want to...
2
2520
by: PamelaDV | last post by:
I am wondering if there is a way to disable the "X" used to close the Access application window? I know how to disable it for individual forms, but I would like to disable it for the application...
4
14133
by: Phoe6 | last post by:
Hi all, I am trying to disable the NIC card (and other cards) enabled in my machine to test diagnostics on that card. I am trying to disable it programmatic using python. I checked python wmi and...
3
11610
by: Pietro | last post by:
Hi all, First of all I'd like to thank you very very much ,as finally after many years of searching,I could find a code to disable/enable the shift key,but actually i cannot use the code as I'm...
8
17205
by: sebouh181 | last post by:
I am writing to registry using javascript. var wsh = new ActiveXObject("WScript.Shell"); var key = "HKLM\\Software\\Neos\\2.0\\LightMode\\my-script"; wsh.RegWrite (key, 1999, "REG_SZ"); when i...
2
4351
by: Naushad | last post by:
Hi all, I am using the countinous form. I want to Enable/Disable the some fields for perticular records as per the following condition when open the form. I have written this code in "On Current...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.