我设置了一个记录器,但我无法弄清楚只有在命令失败或生成标准错误时如何记录。
def RunAndLog(self, command, logf)
p = subprocess.Popen(command, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
logger = logging.getLogger('check rel')
logger.setLevel(logging.ERROR)
filehand = logging.handlers.RotatingFileHandler(logFile, mode='a',maxBytes=0,
backupCount=0, encoding=None, delay=0)
filehand.setLevel(logging.ERROR)
lformat = logging.Formatter('%(message)s')
filehand.setFormatter(lformat)
logger.addHandler(filehand)
logger.info(stdout)
logger.error(stderr)
logger.removeHandler(filehand)
我称它为列出目录。就像是,
self.RunAndLog(["dir","%s",pathtodir], "test.log")
test.log
包含两种情况的消息,即目录不存在时以及目录不存在时...这是一个示例:
#case where dir doesn't exist
dir: /path/to/dir/dir1: No such file or directory
# case where it does
bin lib include src test python doc
如何test.log
仅在目录不存在或dir
命令以非零退出状态失败的情况下记录消息?
谢谢...