subprocess.call logger info and error for stdout and stderr respectively

后端 未结 1 1907
孤独总比滥情好
孤独总比滥情好 2021-02-14 14:06

I have a logger. something like this:

import logging

logger = logging.getLogger(\'myApp\')
hdlr = logging.FileHandler(\'myApp.log\')
logger.addHandler(hdlr)
log         


        
1条回答
  •  隐瞒了意图╮
    2021-02-14 14:59

    Use subprocess.Popen() and call .communicate() on the returned process object:

    p = subprocess.Popen(["someCommand"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    
    if stdout:
        logger.info(stdout)
    if stderr:
        logger.error(stderr)
    

    0 讨论(0)
提交回复
热议问题