【发布时间】:2017-08-07 07:43:39
【问题描述】:
我正在尝试使用 libvirt python API “lookupbyname()”检查域是否存在。如果域不存在,它会在控制台上打印一条错误消息“Domain not found”。 我只需要系统日志中的错误或日志。我试过重定向标准错误和标准输出。但是,它没有任何作用。我还尝试过使用 https://libvirt.org/logging.html 中描述的 libvirt 日志记录设置。又没有效果了。 /etc/libvirt/qemu.conf 中的“stdio_handler”标志也设置为“file”。
以下是我的测试代码:
import os, sys
import libvirt
conn = libvirt.open('qemu:///system')
# Find the application in the virsh domain
try:
sys.stdout = open(os.devnull, "w")
sys.stderr = open(os.devnull, "w")
dom = conn.lookupByName('abcd')
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
except Exception as e:
syslog.syslog (syslog.LOG_ERR, 'Could not find the domain. ERROR: %s.' % (e))
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
输出:
$ python test.py
libvirt: QEMU Driver error : Domain not found: no domain with matching name 'abcd'
$
有没有办法避免这个控制台打印?
【问题讨论】:
标签: python logging qemu libvirt