我有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
我希望日志消息由stdout而不是stderr返回...有人知道为什么将其作为错误返回吗?
答案 0 :(得分:1)
basicConfig
除其他外,provides a StreamHandler
for the root logger。默认情况下,为new StreamHandler
writes to standard error。