[libvirt-users] Avoiding console prints by Libvirt Qemu python APIs

Daniel P. Berrange berrange at redhat.com
Mon Aug 7 12:07:36 UTC 2017


On Mon, Aug 07, 2017 at 01:15:45PM +0530, swaroop sp wrote:
> Hi,
> 
> I am trying to check if a domain exists by using the libvirt python API
> *"lookupbyname()*". If the domain does not exist, it prints an error
> message on the console saying "*Domain not found*".
> 
> I need the errors or logs only in syslog. I have tried redirecting stderr
> and stdout. But, it doesn't have any effect. I have also tried playing
> around with the libvirt logging settings described in
> https://libvirt.org/logging.html . No effect again. "stdio_handler" flag in
> /etc/libvirt/qemu.conf is set to "file" as well.
> 
> Following is my test code:
> 
> import os, sysimport libvirt
> conn = libvirt.open('qemu:///system')
> # Find the application in the virsh domaintry:
>     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__
> 
> Output:
> 
> $ python test.py
> libvirt: QEMU Driver error : Domain not found: no domain with matching
> name 'abcd'
> $
> 
> Is there a way to avoid this console print?

Use this:

def libvirt_callback(userdata, err):
    pass

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

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|




More information about the libvirt-users mailing list