[Python-Dev] Using logging in the stdlib and its unit tests

Vinay Sajip vinay_sajip at yahoo.co.uk
Wed Dec 8 00:58:54 CET 2010

Robert Kern <robert.kern <at> gmail.com> writes:

> If I had my druthers, I would simply remove the "No handlers could be
> found for logger XXX" message. If we always wrote entire applications
> from the ground up, it makes a lot of sense. The person that writes the
> code that issues logs is the same person that writes the code to configure
> logging for reading. If you add logging in one place, you almost certainly
> want to use it somewhere else and not setting up logging is probably an
> error. But when you want to write reusable  libraries, those roles become
> separated.

Exactly - we do use third-party libraries. When logging was added there was some
debate about whether this one-off message should be printed, and in balance it
was thought better to print this, not least because people would be unfamiliar
with logging (as it was then new) and so be not unlikely to misconfigure it. No
indication of this at all would be double-plus-ungood.

> As a library author, I would dearly love to just add logging liberally
> without placing any additional burden to the users of my library.
> If my users wants to read those logs, he will configure logging. If he
> doesn't, he won't. With the current behavior, I can't do that. If I add
> logging, he has to add code just to silence a message that is meaningless
> to him (after I get the support emails asking if things are broken and
> explain how to silence it). If I add a NullHandler, I remove the ability
> for him to use logging.basicConfig(), the easiest and most straightforward
> way for him to add logging to his application.

You don't remove the ability for him to use basicConfig() - that's another
mistake in your post (you already corrected the other mistake in your
self-response). See my example with mylib.py/myapp.py in another post on this
thread, in response to Antoine.

> This is only my personal anecdotal experience, but the current behavior has 
> always wasted my time and never saved any of my time.

That's because I think you're doing it wrong, or else I've misunderstood your
use case. NullHandler is the way to go, and doesn't preclude the use of
basicConfig() UNLESS you choose to add the NullHandler to the root logger (not
what you're supposed to do - you're supposed to add it to the top-level logger
*of your library*, not the top-level logger of the logging hierarchy.)

See the mylib/myapp example and tell me which it is - your mistake or mine?


Vinay Sajip

More information about the Python-Dev mailing list