What should go to stdout/stderr and why Python logging write everything to stderr?

Thomas Passin list1 at tompassin.net
Tue Jan 3 11:26:52 EST 2023


On 1/3/2023 10:35 AM, c.buhtz at posteo.jp wrote:
> Hello,
> 
> this posting isn't about asking for a technical solution. My intention
> is to understand the design decision Python's core developers made in
> context of that topic.
> 
> The logging module write everything to stderr no matter which logging
> level is used.
> 
> The argparse module does it more differentiated. If arguments are
> mandatory but none are given then argparse produce a short version of
> the usage info; on stderr. If the user request the usage info via "-h"
> it goes to stdout. This is the behavior I would expect.
> 
> Why does logging behave different? DEBUG and INFO imho should go to
> stdout not stderr.

I do not have any special insight into the developers' minds, but here 
is my own reaction.  Logging output is normally not the primary output 
of a program.  For example, you might want to capture the program's 
output and do some processing on it.  In that case you don't want to 
have to parse out the logging text so as to avoid processing it. If not 
to stdout, then where should logging output go?  The only two 
possibilities seem to be stderr, or a known file.

Also, I think it would be confusing to sometimes have logging output go 
to stdout and other times go to stderr.

In the case of argparse, the messages are the intended program output, 
so to me it makes sense that error announcements should go to stderr and 
requests for help information should go to stdout.

There is probably no single right way to do it, but the way it's being 
done makes sense to me, FWIW.

> 
> Of course I could modify the handlers etc. to workaround this. But I
> assume that there was something in mind of the Python developers when
> they decided that.
> 
> My goal is not to divide between the use of print() or logging.info()
> in my own code. This would mess up a lot.



More information about the Python-list mailing list