只是想在这件事上动动脑筋。我有几个子进程正在运行,我想将 stderr 和 stdout 打印到文件中。到目前为止,我已经这样做了:
def write_to_stderr_log(process):
stderr= open("stderr.log", "w")
proc_err = process.communicate()
print >> stderr, proc_err
stderr.close()
def write_to_stdout_log(process):
stdout = open("stdout.log", "w")
proc_out = process.communicate()
print >> stdout, proc_out
stdout.close()
def logger():
logger = logging.getLogger('error_testing')
hdlr = logging.FileHandler('error.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
logger.error('We have a problem')
logger.debug('debugging')
logger.info('some info')
logger()
proc = subprocess.Popen(['FastTree -nt test.fasta'], bufsize=512, stdin = None, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell=True)
write_to_stderr_log(proc)
write_to_stdout_log(proc)
这是最好的方法吗?如果我有多个进程,我猜它会重写日志文件,所以这可能是一个问题。在这里欣赏一些建议。谢谢