Python 函数捕获子进程 stdout 和 stderr 到日志文件
Python function capturing subprocess stdout and stderr to log file
这里用少量代码完成了很多事情。我会尽量保持简洁。
我有一个 python 函数,它运行一个外部程序并将 stdout 和 stderr 发送到一个日志文件。
我正在使用 doctest 来测试该功能。我需要测试输出捕获功能。下面的代码显示了我尝试编写函数和测试。测试失败,没有任何内容写入日志文件。我不确定问题是出在测试中还是出在被测代码中,或者两者都有。建议?
from __future__ import print_function
import subprocess
def run(command_line, log_file):
"""
# Verify stdout and stderr are both written to log file in chronological order
>>> run("echo text to stdout; echo text to stderr 1>&2", "log")
>>> f = open("log"); out = f.read(); f.close()
>>> print(out.strip())
text to stdout
text to stderr
"""
command_line = "set -o pipefail; " + command_line + " 2>&1 | tee " + log_file
# Run command. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError
subprocess.check_call(command_line, shell=True, executable="bash")
测试结果:
$ python -m doctest testclass.py
text to stdout
text to stderr
**********************************************************************
File "testclass.py", line 10, in testclass.run
Failed example:
print(out.strip())
Expected:
text to stdout
text to stderr
Got:
<BLANKLINE>
**********************************************************************
1 items had failures:
1 of 3 in testclass.run
***Test Failed*** 1 failures.
因为使用 shell=True
执行 subprocess.check_call
,使用 2 stdout/stderr 重定向和 tee
并不是执行命令和捕获输出的最佳方式(实际上它是最接近最差方式),我对它失败并不感到惊讶。
我的解决方案是为初学者删除 set -o pipefail
(您不需要在此处检查 return 代码)并将两个命令都放在括号中,否则重定向/tee 仅适用于最后一个一个(老实说,我仍然很困惑为什么你没有得到任何输出):
command_line = "(" + command_line + ") 2>&1 | tee " + log_file
如果您必须恢复 pipefail
的东西,请在括号内进行:
command_line = "(set -o pipefail; " + command_line + ") 2>&1 | tee " + log_file
这里用少量代码完成了很多事情。我会尽量保持简洁。
我有一个 python 函数,它运行一个外部程序并将 stdout 和 stderr 发送到一个日志文件。
我正在使用 doctest 来测试该功能。我需要测试输出捕获功能。下面的代码显示了我尝试编写函数和测试。测试失败,没有任何内容写入日志文件。我不确定问题是出在测试中还是出在被测代码中,或者两者都有。建议?
from __future__ import print_function
import subprocess
def run(command_line, log_file):
"""
# Verify stdout and stderr are both written to log file in chronological order
>>> run("echo text to stdout; echo text to stderr 1>&2", "log")
>>> f = open("log"); out = f.read(); f.close()
>>> print(out.strip())
text to stdout
text to stderr
"""
command_line = "set -o pipefail; " + command_line + " 2>&1 | tee " + log_file
# Run command. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError
subprocess.check_call(command_line, shell=True, executable="bash")
测试结果:
$ python -m doctest testclass.py
text to stdout
text to stderr
**********************************************************************
File "testclass.py", line 10, in testclass.run
Failed example:
print(out.strip())
Expected:
text to stdout
text to stderr
Got:
<BLANKLINE>
**********************************************************************
1 items had failures:
1 of 3 in testclass.run
***Test Failed*** 1 failures.
因为使用 shell=True
执行 subprocess.check_call
,使用 2 stdout/stderr 重定向和 tee
并不是执行命令和捕获输出的最佳方式(实际上它是最接近最差方式),我对它失败并不感到惊讶。
我的解决方案是为初学者删除 set -o pipefail
(您不需要在此处检查 return 代码)并将两个命令都放在括号中,否则重定向/tee 仅适用于最后一个一个(老实说,我仍然很困惑为什么你没有得到任何输出):
command_line = "(" + command_line + ") 2>&1 | tee " + log_file
如果您必须恢复 pipefail
的东西,请在括号内进行:
command_line = "(set -o pipefail; " + command_line + ") 2>&1 | tee " + log_file