logging from several independent classes

S

Sandy

Hi all,
I was going through the last example in logging docs:
http://docs.python.org/library/logging.html#using-logging-in-multiple-modules

It explains how to log from multiple classes. The example works fine
as long as the logger names are in a heirarchy
(spam_application.auxiliary.Auxiliary, spam_application.auxiliary,
spam_application).
Just configure at top level and the child classes automatically
inherit the config. If I change the name that don't have any
heirarchy, it fails to log properly. My question is: Is it possible to
configure logging in the main script that calls different independent
classes (say A, B, C) and still the loggers in A, B, C inherit the
config from the main script?

For example in class A, all I want to do is
import logging
log = logging.getLogger("A")

and log should be configured according to who ever instantiates A.

Cheers,
Sandeep
 
V

Vinay Sajip

Sandy said:
Hi all,
I was going through the last example in logging docs:
http://docs.python.org/library/logging.html#using-logging-in-multiple-modules

It explains how to log from multiple classes. The example works fine

The documentation you pointed to is about logging from multiple modules. Are you
sure you understand the distinction between classes and modules?
as long as the logger names are in a heirarchy
(spam_application.auxiliary.Auxiliary, spam_application.auxiliary,
spam_application).
Just configure at top level and the child classes automatically
inherit the config. If I change the name that don't have any
heirarchy, it fails to log properly. My question is: Is it possible to
configure logging in the main script that calls different independent
classes (say A, B, C) and still the loggers in A, B, C inherit the
config from the main script?

For example in class A, all I want to do is
import logging
log = logging.getLogger("A")

All named loggers (including "A", "B" etc.) inherit from the root logger,
obtained by doing

root_logger = logging.getLogger()

or

root_logger = logging.getLogger("")

So you can use that to configure your handlers, level etc.

Regards,

Vinay Sajip
 
S

Sandy

All named loggers (including "A", "B" etc.) inherit from the root logger,
obtained by doing

root_logger = logging.getLogger()

or

root_logger = logging.getLogger("")

Somehow I missed this in the docs.
Thanks Vinay.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top