Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

Python-requests中, 这一段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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...