问题描述
我想从程序中记录消息,而不是从它使用的库中记录消息.我可以像这样禁用/更改单个库的日志记录级别:
I want logging messages from my program, but not from the libraries it uses. I can disable / change the logging level of individual libraries like this:
logging.getLogger('alibrary').setLevel(logging.ERROR)
问题是,我的程序使用了很多库,而库本身也使用了很多库.因此,对每个库单独执行此操作会很麻烦.有更好的方法吗?
The problem is, my program uses lots and lots of libraries, which use lots themselves. So doing this individually for every library is a big chore. Is there a better way to do this?
推荐答案
您可以将根记录程序的级别设置为例如ERROR
,然后有选择地为您自己的代码设置更多详细级别:
You could set the root logger's level to e.g. ERROR
and then selectively set more verbose levels for your own code:
logging.getLogger().setLevel(logging.ERROR)
然后假设您所使用的库在日志记录方面表现良好,则其记录器的有效级别应有效为ERROR
,就像您分别设置每个库一样.
then assuming the libraries you use are well-behaved with regard to logging, the effective levels of their loggers should effectively be ERROR
just as if you had set each one individually.
这篇关于禁用所有库的日志消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!