Search code examples
pythonloggingdefaultstderr

Why Python logging writes to stderr by default?


Related to Does Python logging write to stdout or stderr by default? but I want to know WHY it defaults to stderr instead WHAT is the default.

For me it isn't really obvious why stderr is the default. I noticed that something is wrong when I run python script.py | tee out.log and ended up with empty log file. Now I know that It can be solved by either python script.py 2>&1 | tee out.log or using stream parameter:

logging.basicConfig(stream=sys.stdout)

After that event it seems reasonable to me to change default stream to stdout in each script to avoid being surprised again. Is that a good practice? Will I miss something?


Solution

  • You probably don't want to change the default to stdout. The reason for that is stderr is meant to be an output for all messages that are just some internal and/or debugging program information like logs and errors, rather than actual output, the result of the program.