Logging with Logger hierarchies

Vinay Sajip vinay_sajip at yahoo.co.uk
Fri Oct 20 09:58:52 EDT 2006


> I delved in the code and found that when my logger "a.b.c" is created,
> it doesn't create Loggers for "a.b" or "a" but rather instances of a
> PlaceHolder class. If I actually call logging.getLogger("a.b") and
> logging.getLogger("a")  then the PlaceHolders get converted to Loggers
> and the hierarchy suddenly works.
>
> My feeling is that either the PlaceHolder objects should be modified to
> make them work properly in the hierarchy, or probably more simply, they
> should just be replaced by Loggers immediately when the hierarchy is
> first created. In that case, I believe that my expected behaviour would
> "just work". Now I am beginning to wonder if I have missed something,
> ie is there some reason why this is not a good idea and hasn't been
> done? Otherwise, what do people generally think? Is it as simple as I
> haven't set something up right, or do I have to declare the
> intermediate loggers, or does having multiple layers just suck!!!

There is nothing wrong with the way you are naming your loggers - and
there is nothing wrong with the way PlaceHolders work, either. They do
just "work", AFAIK. If there were something wrong with your config
file, no handler might get configured on the root logger, which would
lead to the observed behaviour when you log to "a.b.c".

With the simple script

import logging

logging.basicConfig(level=logging.DEBUG,
    format="%(name)s: %(levelname)-5s: %(message)s")
logging.getLogger("a.b.c").debug("Hello, world!")

I get the output

a.b.c: DEBUG: Hello, world!

which is as expected.

Regards,

Vinay Sajip




More information about the Python-list mailing list