导入的模块添加了不必要的日志记录.如何抑制它? [英] Imported module adding unwanted logging. How can it be suppressed?

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

问题描述

在导入需要使用的模块后,我看到了额外的日志消息.我正在尝试找出正确的方法来阻止这种情况的发生.以下代码最能说明问题:

I'm seeing extra logging messages after I've imported a module I need to use. I'm trying to work out the correct way to stop this happening. The following code shows the issue best:

import os
import logging
import flickrapi

class someObject:
    def __init__(self):
        self.value = 1
        logger = logging.getLogger(__name__)
        print logger.handlers
        logger.info("value = " + str(self.value))

def main():
    # Set up logging
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter('[%(asctime)-15s] %(name)-8s %(levelname)-6s %message)s')
    fh = logging.FileHandler(os.path.splitext(os.path.basename(__file__))[0]+".log")
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    logger.debug("Debug message")
    logger.info("Info message")

    thingy = someObject()

if __name__ == "__main__":
    main()

通过flickrapi导入,我看到以下输出:

With the flickrapi import I see the following output:

DEBUG:__main__:Debug message
[2013-05-03 12:10:47,755] __main__ INFO   Info message
INFO:__main__:Info message
[<logging.FileHandler instance at 0x1676dd0>, <logging.StreamHandler instance at 0x1676ea8>]
[2013-05-03 12:10:47,755] __main__ INFO   value = 1
INFO:__main__:value = 1

移除flickrapi导入后,我看到正确的输出:

With the flickrapi import removed I see the correct output:

[2013-05-03 12:10:47,755] __main__ INFO   Info message
[<logging.FileHandler instance at 0x1676dd0>, <logging.StreamHandler instance at 0x1676ea8>]
[2013-05-03 12:10:47,755] __main__ INFO   value = 1

这是我第一次使用日志记录,这让我有些困惑.我已经阅读了几次文档,但是我认为我的理解中缺少一些东西.

This is my first time of using logging and it's got me a little stumped. I've read the documentation a couple of times but I think I'm missing something in my understanding.

logging.Logger.manager.loggerDict,还有其他记录器,但它们的每个.handlers都是空的. __main__记录器仅具有我添加的两个处理程序,因此这些消息来自何处?

Looking at logging.Logger.manager.loggerDict, there are other loggers but each of their .handlers is empty. The __main__ logger only has the two handlers I've added so where do these messages come from?

关于我如何解决此问题的任何指示,在我碰壁时将不胜感激.

Any pointers as to how I can solve this would be much appreciated as I've hit a wall.

谢谢

推荐答案

这是您使用的flickrapi库中的错误.它在它是__init__.py ,这是错误的为库执行此操作,因为它向根记录器添加了默认为stderr的StreamHandler.

This is a bug in the flickrapi library you are using. It is calling logging.basicConfig() in it's __init__.py which is the wrong thing to do for a library since it adds a StreamHandler defaulting to stderr to the root logger.

您可能应该与作者一起打开错误报告. 在python日志记录文档中有一个关于如何库应配置日志记录.

You should probably open a bug report with the author. There is a HOWTO in the python logging docs on how libraries should configure logging.

要在解决此问题之前解决此问题,您应该可以执行以下操作:

To work around this issue until the bug is fixed you should be able to do the following:

# at the top of your module before doing anything else
import flickrapi
import logging
try:
    logging.root.handlers.pop()
except IndexError:
    # once the bug is fixed in the library the handlers list will be empty - so we need to catch this error
    pass

这篇关于导入的模块添加了不必要的日志记录.如何抑制它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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