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

Nick Coghlan ncoghlan at gmail.com
Wed Dec 8 15:52:52 CET 2010


On Thu, Dec 9, 2010 at 12:27 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Wed, 8 Dec 2010 12:15:53 +0000 (UTC)
> Vinay Sajip <vinay_sajip at yahoo.co.uk> wrote:
>> See my comments to Nick Coghlan's post about getting messages out when you can't
>> raise an exception. I think the case is not as common as you suggest (because in
>> many instances, you would raise an exception to signal an error).
>
> I'm not talking specifically about exceptions, but about errors in
> general. If the case wasn't common, I'm not sure why the error() and
> critical() methods would exist at all.
>
> (of course I'm assuming error() is meant to output error messages. If
> that's a wrong interpretation then I'm a bit puzzled :-))

As I see it, there aren't many cases at the *library* level where
logging errors is more appropriate than raising exceptions:
- operations where there is no outer exception handler, and the
library knows it (e.g. callback processing)
- methods that are guaranteed not to raise exceptions (e.g. top level
event handlers)

Some batch processing operations may also work that way, but those are
a little more questionable (it may be better to report a result
summary and leave it up to the application to decide what to do with
any errors encountered along the way)

However, it *is* the case that logging is currently ill-suited to
tasks that are otherwise handled by writing directly to sys.stderr. I
suspect that has influenced some people's views of its suitability for
use in published libraries, even though it doesn't actually affect
most libraries (I suspect it is a significant chunk of what underlay
my reservations about using logging in library code). Since the
standard library *does* include modules that currently write to
stderr, then adjusting logging to make it better suited to this task
will help us as well as anyone else with similar problems.

That said, while I think Vinay's suggested "handler of last resort"
solution is a good one and something we should be doing for 3.2, I'm
also happy to let the idea bake for at least a few weeks.

Cheers,
Nick.

P.S. On a completely unrelated note, has anyone thought about creating
a write-only TextIO stream that outputs received writes via the
logging module?

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list