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

后端 未结 1 1125
再見小時候
再見小時候 2021-02-14 14:11

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:41

    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)
提交回复
热议问题