简体   繁体   English

如何使Yum Python API模块输出静音?

[英]How can I silence the Yum Python API module output?

Is there a way to prevent the yum API from creating output when it runs? 有没有一种方法可以防止yum API在运行时创建输出?

For instance, if I run this simple code (with yum-3.4.3-132.el7.centos.0.1): 例如,如果我运行以下简单代码(使用yum-3.4.3-132.el7.centos.0.1):

import yum
yb = yum.YumBase()
yb.repos.populateSack(mdtype='metadata', cacheonly=0)

I get the following output: 我得到以下输出:

Loaded plugins: fastestmirror

I don't want to blackhole all output, just the output from the yum API. 我不想黑所有的输出,只是yum API的输出。

Piggybacking on thescouser89's answer here. 在此处带scouser89的答案。

I'm not sure if this is an exhaustive list of every Yum logger, but I think it's pretty close. 我不确定这是否是每个Yum记录器的详尽列表,但我认为它非常接近。 You can disable all of them before you start calling into Yum and it will be completely silent. 您可以在开始调用Yum之前禁用所有功能,这将是完全静音的。

import logging
from yum.logginglevels import __NO_LOGGING

yumLoggers = ['yum.filelogging.RPMInstallCallback','yum.verbose.Repos', 'yum.verbose.plugin', 
'yum.Depsolve', 'yum.verbose', 'yum.plugin', 'yum.Repos', 'yum', 'yum.verbose.YumBase', 
'yum.filelogging', 'yum.verbose.YumPlugins', 'yum.RepoStorage', 'yum.YumBase', 
'yum.filelogging.YumBase', 'yum.verbose.Depsolve']

for loggerName in yumLoggers:
    logger = logging.getLogger(loggerName)
    logger.setLevel(__NO_LOGGING)

You can also override some of the progress bars / event loggers from the various RPM transactions by making a class that inherits RPMBaseCallback and passing that into the various transaction functions (processTransaction, etc). 您还可以通过创建一个继承RPMBaseCallback的类并将其传递给各种事务功能(processTransaction等)来覆盖各种RPM事务中的某些进度条/事件记录器。 However, if you're disabling all the loggers as I've done above, those functions will not be called. 但是,如果像我上面那样禁用所有记录器,则不会调用这些功能。

Edit: Upon further inspection, I think the simplest answer is: https://stackoverflow.com/a/43625141/619255 编辑:经过进一步检查,我认为最简单的答案是: https : //stackoverflow.com/a/43625141/619255

However, this approach doesn't silence warnings about incomplete transactions, while disabling all of the loggers does. 但是,此方法不会使有关未完成交易的警告消失,而禁用所有记录器。

I looked into the RHEL7 yum code and I believe you can do that in your code like so: 我查看了RHEL7 yum代码,并相信您可以在代码中执行以下操作:

import logging
logger = logging.getLogger("yum.verbose.YumPlugins")
# Only print critical logs from yum
logger.setLevel(logging.CRITICAL)

# The "Loaded plugins:" text is printed as a debug. So anything above
# logging.DEBUG should silence it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM