1
我有一个日志记录器设置,但我无法弄清楚如何仅在命令失败或生成stderr时才记录日志。只使用python logger将日志文件记录到日志文件中
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
命令失败且非零退出状态的情况下?
谢谢...
它在那里。从顶部 – iamauser
排行#3好抓住,但这是一个错字,当我粘贴...现在编辑... – iamauser
我怀疑,因为没有调用它会导致例外。 –