日志记录 - 如何忽略导入的模块日志? [英] logging - how to ignore imported module logs?

查看:162
本文介绍了日志记录 - 如何忽略导入的模块日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何忽略来自导入的模块的日志条目(不是由我写的)?



设置:

  import logging 
import< someOtherModule>

logging.basicConfig(level = logging.INFO)

class myClass:
...
def some_method(self):
log.info('调用模块')
someOtherModule.function()
logging.info('stuff happen')

如果__name__ ==__main__:
a = myClass()。some_method()

日志:

  INFO:root:调用模块
INFO:< someOtherModule>。< some dependency> <随机依赖性消息>
INFO:root:发生事情

我如何摆脱这个中间消息?



在查看日志记录文档或谷歌搜索后,我无法找到答案。

我发现这个答案,但解决方法对我来说似乎不起作用。



对于好奇的实际日志条目是:

  INFO :requests.packages.urllib3.connectionpool:启动新的HTTPS连接(1):< address here> 


解决方案

我们有不同的日志记录级别,如: p>

 'debug':logging.DEBUG,
'info':logging.INFO,
'warning' log.WARNING,
'error':logging.ERROR,
'critical':logging.CRITICAL

所以,如果你不想看到 INFO 级别,只需提高级别,如:

  logging.basicConfig(level = logging.CRITICAL)

不要忘记将您想要的信息的日志级别更改为 CRITICAL



OR: p>

尝试这样:

  ... 
logger = logging .getLogger()
...
logger.disabled = True
someOtherModule.function()
logger.disabled = False
...

希望你的问题解决。 :)


How to ignore log entries from imported modules (not written by me)?

The setup:

import logging    
import <someOtherModule>

logging.basicConfig(level=logging.INFO)    

class myClass: 
   ...
   def some_method(self):
       logging.info('calling module')
       someOtherModule.function()
       logging.info('stuff happened')

if __name__ == "__main__":
    a = myClass().some_method()

The Log:

INFO:root:calling module
INFO:<someOtherModule>.<some dependency> <random dependency message here>
INFO:root:stuff happened

How can I get rid of that middle message?

I was not able to find an answer after looking at the logging documentation or by googling.
I found this answer but the workaround does not seem to work for me.

For the curious ones the actual log entry is:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): <address here>  

解决方案

We have different levels for logging, like:

      'debug': logging.DEBUG,
      'info': logging.INFO,
      'warning': logging.WARNING,
      'error': logging.ERROR,
      'critical': logging.CRITICAL

So, if you don't want to see INFO level, just raise the level, like:

logging.basicConfig(level=logging.CRITICAL)

Don't forget to change level of log for your wanted information to CRITICAL.

OR:

Try this:

...
logger = logging.getLogger()
...
logger.disabled = True
someOtherModule.function()
logger.disabled = False
...

Hope your issue solves. :)

这篇关于日志记录 - 如何忽略导入的模块日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆