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

Robert Kern robert.kern at gmail.com
Tue Dec 7 23:59:36 CET 2010

On 12/7/10 2:26 PM, Vinay Sajip wrote:
> This issue was brought to my notice today:
> http://bugs.python.org/issue10626
> and reference was made in the comments to possible obstacles facing stdlib
> maintainers who might wish to use logging in the stdlib and in its unit tests.
>> From my perspective and as mentioned in the logging documentation, library code
> which uses logging should add a NullHandler instance to any top-level logger,
> which will avoid any "No handlers could be found for logger XXX" message if no
> logging handlers have been set up.

I've done that before in my own library code, then quickly realized that it was 
a bad idea. Adding a NullHandler silently prevents logging.basicConfig() from 
working. Embedding that kind of restriction into library code (usually by a mere 
import!) made users unhappy and added an additional burden on the library 
developers who had to make sure to do that everywhere they used logging.

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.

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.

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

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-Dev mailing list