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

Vinay Sajip vinay_sajip at yahoo.co.uk
Wed Dec 8 09:51:44 CET 2010

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

> I really don't understand how this view can be consistent with the
> practice of adding NullHandler to loggers. If this message is so important
> to prevent misconfiguration, then why should a library author decide to
> silence it for his users?

Because the application developer knows more about the end-user audience for
their application, they are better placed to know how logging should work for
their application. It's not an error for a particular application developer to
decide that nothing should be produced by logging for a particular application;
they (particularly when casual users) would be confused by the misconfiguration
message due to logging by a library they're using.

The library author's users are the application developers who use the library,
not the end users who use their applications. Sometimes they're the same people,
I know, but I just think of them as wearing different hats :-)

> [...] I strongly suspect that almost all configurations include a 
> catch-all root logger and that most of those *only* consist of that
> root logger.

That doesn't seem right: your comment might be conflating loggers with handlers.
The common pattern would be (or should be) to name loggers according to __name__
in the modules which use logging, but only configure *handlers* for the root
logger. That way, logging messages indicate their origin (because of the
__name__ convention) but you only need to add handlers at the root logger to
capture all the logging information.

> Same mistake. I intended the correction to apply to all such statements in
> my post.

Oh, right. I thought this was a different case.

> I think that boilerplate should be minimized. If using getLogger() should
> almost always be followed by adding a NullHandler, then it should be the
> default behavior. The easiest way to achieve this effect is to simply not
> issue the warning message.

getLogger() should NOT "almost always be followed by adding a NullHandler". For
example, in Django, only the logger named "django" would have that handler
added; no other Django logger (e.g. "django.db.models") would need to have that
handler added.


Vinay Sajip

More information about the Python-Dev mailing list