在 Python 中使用 subprocess.check_output 捕获日志记录输出
Capture logging output using subprocess.check_output in Python
我正在 运行安装 Telegram 机器人,它基本上是我制作的贝叶斯分类器的接口。我发出命令在远程服务器上开始测试,测试人员最初使用 logging
模块将有关测试的信息打印到标准输出。当我尝试通过 subprocess.check_output
生成子进程时,我注意到 stdout 和 stderr 都是空字节字符串。我通过用 print
函数替换最后一个真正重要的 logging.info
调用来解决这个问题。我猜 logging
没有写在 subprocess.check_output
期望的同一个标准输出上?我做错了什么吗?
编辑
根据要求,我添加了更多关于我实际编写的代码的上下文 运行。我不知道这是否重要,但为了完整起见,我将添加我用于 运行 机器人的框架是 pyrogram。
可以找到被机器人调用的测试人员here, while the code of the bot calling that script is here
I'm guessing is that logging does not write on the same stdout that subprocess.check_output is expecting?
logging
根本不写入标准输出(默认情况下);它写入标准错误。
您将需要通过添加 stderr
参数将您的 stderr 重定向到 stdout:subprocess.check_output(..., stderr=subprocess.STDOUT)
或者您将需要使用其他子进程函数之一,例如 run
可以访问 stderr。
文档确实说:
The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle
我正在 运行安装 Telegram 机器人,它基本上是我制作的贝叶斯分类器的接口。我发出命令在远程服务器上开始测试,测试人员最初使用 logging
模块将有关测试的信息打印到标准输出。当我尝试通过 subprocess.check_output
生成子进程时,我注意到 stdout 和 stderr 都是空字节字符串。我通过用 print
函数替换最后一个真正重要的 logging.info
调用来解决这个问题。我猜 logging
没有写在 subprocess.check_output
期望的同一个标准输出上?我做错了什么吗?
编辑
根据要求,我添加了更多关于我实际编写的代码的上下文 运行。我不知道这是否重要,但为了完整起见,我将添加我用于 运行 机器人的框架是 pyrogram。
可以找到被机器人调用的测试人员here, while the code of the bot calling that script is here
I'm guessing is that logging does not write on the same stdout that subprocess.check_output is expecting?
logging
根本不写入标准输出(默认情况下);它写入标准错误。
您将需要通过添加 stderr
参数将您的 stderr 重定向到 stdout:subprocess.check_output(..., stderr=subprocess.STDOUT)
或者您将需要使用其他子进程函数之一,例如 run
可以访问 stderr。
文档确实说:
The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle