我的老板让我查看一些旧代码,其中所有内容都被发送到stderr。我知道stderr应该包含警告和错误信息,但是什么情况下它们应该真正地发送到stdout呢?
这个程序是一个服务。它发送到stderr的一些消息是:
if pid is None:
message = "pidfile {0} is not running. \n"
sys.stderr.write(message.format(self.pidfile))
所以这个是输出,对吧?关于成功或失败的消息应该发送到标准输出(stdout),因为它不是错误吗?
然后我还有另一个异常:
except OSError as err:
sys.stderr.write('fork #1 failed: {0}\n'.format(err))
sys.exit(1)
这个例子是一个异常情况,所以应该被发送到stderr,因为它是一个错误?还是应该发送到stdout,因为它只是输出一条消息,说明程序失败了?
我知道stdout和stderr是什么——在提问之前我读了很多。我的问题在于能否确定哪些消息应该发送到stderr——只有致命错误,还是任何类型的错误消息都应该发送到stderr?
这两个示例都是错误消息,但其中一个只是“程序没有运行”。我很难选择这种消息应该发送到哪里。