Wim*_*nen 4 python logging command-line
我有一个python脚本,需要执行几个命令行实用程序.stdout输出有时用于进一步处理.在所有情况下,我想记录结果并在检测到错误时引发异常.我使用以下函数来实现此目的:
def execute(cmd, logsink):
logsink.log("executing: %s\n" % cmd)
popen_obj = subprocess.Popen(\
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = popen_obj.communicate()
returncode = popen_obj.returncode
if (returncode <> 0):
logsink.log(" RETURN CODE: %s\n" % str(returncode))
if (len(stdout.strip()) > 0):
logsink.log(" STDOUT:\n%s\n" % stdout)
if (len(stderr.strip()) > 0):
logsink.log(" STDERR:\n%s\n" % stderr)
if (returncode <> 0):
raise Exception, "execute failed with error output:\n%s" % stderr
return stdout
Run Code Online (Sandbox Code Playgroud)
"logsink"可以是任何带有日志方法的python对象.我通常使用它来将日志记录数据转发到特定文件,或将其回显到控制台,或两者,或其他...
这非常好,除了三个问题,我需要更多细粒度控制而不是communication()方法提供:
如果您只想将输出放在文件中以供以后评估,则可以重定向到文件.
您已经定义了stdout =/stderr =方法执行的进程的stdout/stderr.
在您的示例代码中,您只需重定向到脚本当前out/err分配.
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
sys.stdout和sys.stderr只是文件类对象.正如sys.stdout上的文档文档所述,"任何对象都是可以接受的,只要它有一个带有字符串参数的write()方法."
f = open('cmd_fileoutput.txt', 'w')
subprocess.Popen(cmd, shell=True, stdout=f, stderr=f)
Run Code Online (Sandbox Code Playgroud)
所以你只需要给它一个带有write方法的类来重定向输出.
如果你想要控制台输出和文件输出可能是一个类来管理输出.
一般重定向:
# Redirecting stdout and stderr to a file
f = open('log.txt', 'w')
sys.stdout = f
sys.stderr = f
Run Code Online (Sandbox Code Playgroud)
制作重定向类:
# redirecting to both
class OutputManager:
def __init__(self, filename, console):
self.f = open(filename, 'w')
self.con = console
def write(self, data):
self.con.write(data)
self.f.write(data)
new_stdout = OutputManager("log.txt", sys.stdout)
Run Code Online (Sandbox Code Playgroud)
交错取决于缓冲,因此您可能会或可能不会得到您期望的输出.(您可以关闭或减少使用的缓冲,但我不记得此刻的情况)