Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
避免使用Libvirt-Qemu-python API打印控制台_Python_Logging_Qemu_Libvirt - Fatal编程技术网

避免使用Libvirt-Qemu-python API打印控制台

避免使用Libvirt-Qemu-python API打印控制台,python,logging,qemu,libvirt,Python,Logging,Qemu,Libvirt,我试图通过使用libvirt python API“lookupyname()”来检查域是否存在。如果域不存在,它会在控制台上打印一条错误消息,说明“未找到域”。 我只需要syslog中的错误或日志。我已尝试重定向stderr和stdout。但是,它没有任何效果。我还尝试过使用中描述的libvirt日志记录设置。再次无效。/etc/libvirt/qemu.conf中的“stdio_handler”标志也设置为“file” 以下是我的测试代码: import os, sys import lib

我试图通过使用libvirt python API“lookupyname()”来检查域是否存在。如果域不存在,它会在控制台上打印一条错误消息,说明“未找到域”。 我只需要syslog中的错误或日志。我已尝试重定向stderr和stdout。但是,它没有任何效果。我还尝试过使用中描述的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'
$

有没有办法避免这种控制台打印?

这是libvirt的一个历史性设计错误,不幸的是,如果不中断依赖此mis功能的应用程序的compat,我们无法删除它。因此,您需要使用手动关闭到控制台的打印

def libvirt_callback(userdata, err):
    pass

libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)

这是libvirt的一个历史性设计错误,不幸的是,如果不中断依赖于此mis功能的应用程序的compat,我们就无法删除它。因此,您需要使用手动关闭到控制台的打印

def libvirt_callback(userdata, err):
    pass

libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)

对于网络查找和其他libvirt python API,我也在处理类似的问题。这就解决了所有这些问题。非常感谢。对于网络查找和其他libvirt python API,我也在处理类似的问题。这就解决了所有这些问题。非常感谢。