| Store | Cart

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

From: Vinay Sajip <vina...@yahoo.co.uk>
Tue, 7 Dec 2010 20:26:06 +0000 (UTC)
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. This applies to stdlib code, too, though it
would be good if a logger namespace could be agreed for stdlib usage. (The
logging package itself uses the logger "py.warnings" to redirect warnings to
logging when configured to do so. Perhaps we could standardize on "py.XXX" for
stdlib usage?)

I would suggest that when unit testing, rather than adding StreamHandlers to log
to stderr, that something like TestHandler and Matcher from this post:

http://plumberjack.blogspot.com/2010/09/unit-testing-and-logging.html

This will allow assertion checking of logged messages without resorting to
StringIO, getvalue() etc. If people think it's a good idea, I can add the
TestHandler/Matcher classes to the unit test infrastructure (they wouldn't
become part of the public API, at least until 3.3, but I presume they could be
used in the stdlib unit tests).

On the question of using logging in the stdlib and its unit tests, I would like
to throw this open to python-dev: is there anyone here who wants to use logging
but feels that they can't because of some shortcoming in logging? AFAIK there
should be no obstacles. The preferred approach in stdlib code is as I have
outlined above: add a NullHandler to top-level loggers to avoid
misconfiguration, document the logger names you use, and avoid adding any other
handlers by default. For unit testing, add a TestHandler (or equivalent) to your
top-level logger at the start of the test, make any assertions you need to
regarding what has been logged, remove the handler and close it at the end of
the test. I don't believe there is any inherent conflict or incompatibility
between logger usage in the stdlib, use of logging assertions in unit tests and
use of handlers by application code, but I am happy to have any mistake on my
part pointed out.

>From what I've seen, concurrent.futures adds a StreamHandler which is removed in
the unit test and replaced by StreamHandler pointing to a different stream.
This, I believe, should be changed in line with what I've said above.

Comments?

Regards,

Vinay Sajip

_______________________________________________
Python-Dev mailing list
Pyth...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-dev-ml%40activestate.com

Recent Messages in this Thread
Vinay Sajip Dec 07, 2010 08:26 pm
Antoine Pitrou Dec 07, 2010 08:50 pm
Łukasz Langa Dec 07, 2010 09:04 pm
Antoine Pitrou Dec 07, 2010 09:19 pm
Łukasz Langa Dec 07, 2010 10:33 pm
Vinay Sajip Dec 07, 2010 11:45 pm
Antoine Pitrou Dec 08, 2010 12:09 am
Nick Coghlan Dec 08, 2010 01:00 am
Vinay Sajip Dec 08, 2010 01:51 am
Nick Coghlan Dec 08, 2010 07:28 am
Vinay Sajip Dec 08, 2010 08:32 am
Nick Coghlan Dec 08, 2010 10:29 am
Paul Moore Dec 08, 2010 01:02 pm
Antoine Pitrou Dec 08, 2010 09:41 am
Gregory P. Smith Dec 10, 2010 04:24 am
Vinay Sajip Dec 10, 2010 08:06 am
Glenn Linderman Dec 10, 2010 08:31 am
Bill Janssen Dec 10, 2010 06:16 pm
Vinay Sajip Dec 10, 2010 07:39 pm
Guido van Rossum Dec 10, 2010 08:31 pm
Antoine Pitrou Dec 10, 2010 08:49 pm
Glenn Linderman Dec 10, 2010 08:57 pm
Bill Janssen Dec 10, 2010 09:30 pm
Vinay Sajip Dec 10, 2010 10:21 pm
Stephen J. Turnbull Dec 11, 2010 02:35 am
Paul Moore Dec 12, 2010 10:26 pm
Glenn Linderman Dec 13, 2010 12:42 am
Robert Kern Dec 13, 2010 01:22 am
Nick Coghlan Dec 13, 2010 03:30 am
Robert Kern Dec 13, 2010 04:13 am
Nick Coghlan Dec 13, 2010 04:27 am
Vinay Sajip Dec 10, 2010 10:16 pm
Terry Reedy Dec 11, 2010 02:42 am
Nick Coghlan Dec 11, 2010 05:06 am
Nick Coghlan Dec 11, 2010 05:24 am
Vinay Sajip Dec 11, 2010 06:21 am
Brett Cannon Dec 11, 2010 07:32 pm
Nick Coghlan Dec 12, 2010 02:44 pm
Vinay Sajip Dec 12, 2010 05:41 pm
Éric Araujo Dec 12, 2010 10:33 pm
Glenn Linderman Dec 13, 2010 12:53 am
Vinay Sajip Dec 11, 2010 06:18 am
Nick Coghlan Dec 11, 2010 08:35 am
Vinay Sajip Dec 11, 2010 09:28 am
Glenn Linderman Dec 11, 2010 09:40 am
Nick Coghlan Dec 11, 2010 10:51 am
Vinay Sajip Dec 11, 2010 12:06 pm
Nick Coghlan Dec 11, 2010 05:18 pm
Tres Seaver Dec 11, 2010 05:24 pm
Nick Coghlan Dec 11, 2010 05:51 pm
Glenn Linderman Dec 11, 2010 06:25 am
Nick Coghlan Dec 11, 2010 08:00 am
Nick Coghlan Dec 11, 2010 08:14 am
Glenn Linderman Dec 11, 2010 09:30 am
Vinay Sajip Dec 11, 2010 11:52 am
Glenn Linderman Dec 11, 2010 07:17 pm
Vinay Sajip Dec 11, 2010 08:52 am
Nick Coghlan Dec 11, 2010 09:07 am
Glenn Linderman Dec 11, 2010 09:44 am
Bill Janssen Dec 10, 2010 09:26 pm
Stephen J. Turnbull Dec 10, 2010 09:47 am
Hrvoje Niksic Dec 10, 2010 10:37 am
Oleg Broytman Dec 10, 2010 11:33 am
Stephen J. Turnbull Dec 10, 2010 04:01 pm
Ethan Furman Dec 11, 2010 04:19 am
Vinay Sajip Dec 08, 2010 01:19 am
Antoine Pitrou Dec 08, 2010 10:03 am
Vinay Sajip Dec 08, 2010 12:15 pm
Paul Moore Dec 08, 2010 01:19 pm
Vinay Sajip Dec 08, 2010 01:57 pm
Fred Drake Dec 08, 2010 02:09 pm
Antoine Pitrou Dec 08, 2010 02:27 pm
Fred Drake Dec 08, 2010 02:43 pm
Nick Coghlan Dec 08, 2010 02:56 pm
Nick Coghlan Dec 08, 2010 02:52 pm
Alexander Belopolsky Dec 08, 2010 04:28 pm
Vinay Sajip Dec 08, 2010 04:46 pm
Nick Coghlan Dec 08, 2010 05:32 pm
Vinay Sajip Dec 08, 2010 07:11 pm
Paul Moore Dec 08, 2010 06:49 pm
Vinay Sajip Dec 08, 2010 07:04 pm
s...@pobox.com Dec 08, 2010 07:14 pm
Vinay Sajip Dec 08, 2010 07:42 pm
Paul Moore Dec 08, 2010 11:06 pm
Vinay Sajip Dec 08, 2010 11:35 pm
Nick Coghlan Dec 09, 2010 05:57 am
Vinay Sajip Dec 08, 2010 02:54 pm
Antoine Pitrou Dec 08, 2010 06:15 pm
Vinay Sajip Dec 08, 2010 07:00 pm
Antoine Pitrou Dec 08, 2010 07:16 pm
Vinay Sajip Dec 08, 2010 07:40 pm
Vinay Sajip Dec 08, 2010 07:44 pm
Terry Reedy Dec 08, 2010 09:09 pm
Vinay Sajip Dec 10, 2010 11:46 am
Antoine Pitrou Dec 10, 2010 11:58 am
Vinay Sajip Dec 10, 2010 12:34 pm
Antoine Pitrou Dec 10, 2010 01:11 pm
Vinay Sajip Dec 10, 2010 01:37 pm
Glenn Linderman Dec 08, 2010 05:51 pm
Gregory P. Smith Dec 10, 2010 04:29 am
Glenn Linderman Dec 10, 2010 08:36 am
Georg Brandl Dec 08, 2010 08:09 am
Antoine Pitrou Dec 08, 2010 09:42 am
Georg Brandl Dec 08, 2010 10:48 am
Antoine Pitrou Dec 08, 2010 11:12 am
Vinay Sajip Dec 08, 2010 12:22 pm
Georg Brandl Dec 08, 2010 01:33 pm
Eric Smith Dec 07, 2010 09:57 pm
Brett Cannon Dec 07, 2010 11:03 pm
Vinay Sajip Dec 08, 2010 12:09 am
Eric Smith Dec 08, 2010 12:57 am
Robert Kern Dec 07, 2010 10:59 pm
Robert Kern Dec 07, 2010 11:21 pm
Barry Warsaw Dec 07, 2010 11:38 pm
Vinay Sajip Dec 08, 2010 12:01 am
Barry Warsaw Dec 08, 2010 01:20 am
Vinay Sajip Dec 07, 2010 11:58 pm
Robert Kern Dec 08, 2010 02:33 am
Vinay Sajip Dec 08, 2010 08:51 am
Robert Kern Dec 08, 2010 04:35 pm
Vinay Sajip Dec 08, 2010 05:43 pm
Robert Kern Dec 08, 2010 08:20 pm
Glenn Linderman Dec 09, 2010 12:50 am
Vinay Sajip Dec 09, 2010 08:26 am
Vinay Sajip Dec 09, 2010 08:39 am
Glenn Linderman Dec 09, 2010 09:29 am
Vinay Sajip Dec 09, 2010 09:52 am
Chris Withers Dec 13, 2010 11:41 pm
Messages in this thread