从 supervisord 子进程的子进程登录
Logging from the children of a supervisord subprocess
我的系统由一个从 supervisord 启动的 python 应用程序组成。我们称它为 A
。 A
启动一个子进程 B
来完成它的一些任务。 A
和 B
都在 Python 中编码,并使用标准 logging
模块将消息输出到控制台。
A
的 stdout
和 stderr
被记录到 supervisord 配置中指定的文件中。这就像一个魅力。现在,我想将 stdout
和 stderr
从 B
传送到与 A
相同的文件中。如何实现?
Python 子进程模块提供读取 children stdout 和 stderr 的工具。您可以捕获它们并将它们发送到您的记录器。下面是 post-execution 版本,它在 child 完成后将所有输出发送到日志。也可以这样做 line-by-line,您可以在 Whosebug 中找到阅读子进程标准输出 line-by-line 的示例。
worker = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
worker.wait()
logger.debug("Subprocess %s stdout: %s", cmdline, worker.stdout.read().decode("utf-8"))
您无需执行任何操作。 B
默认继承 A
的标准流。
如果 A 的标准输出被重定向到一个文件,那么 B
的标准输出会自动写入同一个地方。
我的系统由一个从 supervisord 启动的 python 应用程序组成。我们称它为 A
。 A
启动一个子进程 B
来完成它的一些任务。 A
和 B
都在 Python 中编码,并使用标准 logging
模块将消息输出到控制台。
A
的 stdout
和 stderr
被记录到 supervisord 配置中指定的文件中。这就像一个魅力。现在,我想将 stdout
和 stderr
从 B
传送到与 A
相同的文件中。如何实现?
Python 子进程模块提供读取 children stdout 和 stderr 的工具。您可以捕获它们并将它们发送到您的记录器。下面是 post-execution 版本,它在 child 完成后将所有输出发送到日志。也可以这样做 line-by-line,您可以在 Whosebug 中找到阅读子进程标准输出 line-by-line 的示例。
worker = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
worker.wait()
logger.debug("Subprocess %s stdout: %s", cmdline, worker.stdout.read().decode("utf-8"))
您无需执行任何操作。 B
默认继承 A
的标准流。
如果 A 的标准输出被重定向到一个文件,那么 B
的标准输出会自动写入同一个地方。