logging from several independent classes

Vinay Sajip vinay_sajip at yahoo.co.uk
Wed Oct 28 09:32:06 EDT 2009


Sandy <dksreddy <at> gmail.com> writes:

> 
> 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





More information about the Python-list mailing list