玩蛇网提供最新Python编程技术信息以及Python资源下载!

谁能帮我解释下这段python源码中logging的作用是什么?

实际现象

  • 一段不熟悉的logging用法( 猜测是给库打log 用的 )

预期现象

  • 弄清楚该段 logging 代码的作用原理( 虽然有句注释 # Set default logging handler to avoid "No handler found" warnings. )

相关代码

  • requests.__init__.py中的用法

# Set default logging handler to avoid "No handler found" warnings.
import logging
try:  # Python 2.7+
    from logging import NullHandler
except ImportError:
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass

logging.getLogger(__name__).addHandler(NullHandler())

上下文环境

  • 产品版本: requests最新

  • 操作系统: Linux

  • Python: 2.7.x

意思就是你当前版本的logging模块没有NullHandler的时候就自己写一个,总之保证后续的代码有NullHandler可用

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd13860.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月18日 - 19时34分40秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules