【问题标题】:popen returns error when executing python script containing logging执行包含日志记录的 python 脚本时,popen 返回错误
【发布时间】:2019-11-17 10:37:34
【问题描述】:

我有 2 个 python 脚本,其中 1 个使用子进程执行另一个,见下文:

main.py

import subprocess

command = ['python', 'logging_test.py']
proc1 = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

out, err = proc1.communicate()

print('Output returned from command: {}'.format(out))
print('Error returned from command: {}'.format(err))

logging_test.py

import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('log')
logger.info('hello')

运行 main.py 时,我得到以下输出:

Output returned from command:
Error returned from command: INFO:log:hello

我希望日志消息由标准输出返回,而不是标准错误……有谁知道它为什么会作为错误返回?

【问题讨论】:

  • 标准错误用于诊断和错误消息。

标签: python logging subprocess


【解决方案1】:

【讨论】:

  • 啊,好的,谢谢!在 basicConfig 或 StreamHandler 中设置 stream=sys.stdout 为我解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 2016-09-05
  • 2012-01-06
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
相关资源
最近更新 更多