简体   繁体   English

如何更改未命名记录器的python日志级别?

[英]How to change python log level for unnamed logger?

To change the logging level of a dependent package that properly names its logger log = logging.getLogger(__name__) is easy: logging.getLogger("name.of.package").setLevel(logging.WARNING) . 要更改正确命名其记录器log = logging.getLogger(__name__)的相关软件包的日志记录级别,很容易: logging.getLogger("name.of.package").setLevel(logging.WARNING)

But if the 3rd party package doesn't name their logger and just logs messages using logging.info("A super loud annoying message!") , how do I change that level? 但是,如果第3方程序包没有命名其记录器,而只是使用logging.info("A super loud annoying message!")记录消息,如何更改该级别? Adding the getLogger(...).setLevel(..) doesn't seem to work because the logger isn't named. 由于未命名记录器,因此添加getLogger(...).setLevel(..)似乎不起作用。 Is it possible to change the logging level output of just one package without changing the level for the entire logging module? 是否可以仅更改一个软件包的日志记录级别输出而无需更改整个日志记录模块的级别?

If the logger is not named, it just means it is the default logger. 如果记录器未命名,则仅表示它是默认记录器。 You can get it by calling logging.getLogger() 您可以通过调用logging.getLogger()来获取它

So to set the log level, do this: 因此,要设置日志级别,请执行以下操作:

logging.getLogger.setLevel(logging.INFO)

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

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