调用thrift出现No handlers could be found for logger "thrift.transport.TSocket"

1.问题

使用thrift版本为0.10,在0.8没有这个问题
其中ncTAgent是代码中封装的thrift接口的结构,在thrift服务端没有启动的时候,应该拋错为连接不到。但是拋错的堆栈输出之前,还有一句日志异常错误。这是不期望出现的

>>> from thrift.transport.TSocket import TSocket
>>> from thrift.transport.TTransport import TBufferedTransport
>>> from thrift.protocol.TBinaryProtocol import TBinaryProtocol
>>> from Agent import ncTAgent
>>> socket = TSocket('127.0.0.1', 9202)
>>> transport = TBufferedTransport(socket)
>>> protocol = TBinaryProtocol(transport)
>>> client = ncTAgent.Client(protocol)
>>> conn = transport.open()
No handlers could be found for logger "thrift.transport.TSocket"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/thrift/transport/TTransport.py", line 152, in open
    return self.__trans.open()
  File "/usr/lib64/python2.7/site-packages/thrift/transport/TSocket.py", line 113, in open
    raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('127.0.0.1', 9999)]

2.原因

拋错信息有thrift.transport.TSocket,所以查看源码中对应位置,有以下代码

import logging
...

logger = logging.getLogger(__name__)

class TSocketBase(TTransportBase):
    def _resolveAddr(self):
        ...
    def close(self):
        ...
class TSocket(TSocketBase):
    """Socket implementation of TTransport base."""
        ...
    def open(self):
        if self.handle:
            raise TTransportException(TTransportException.ALREADY_OPEN)
        try:
            addrs = self._resolveAddr()
        except socket.gaierror:
            msg = 'failed to resolve sockaddr for ' + str(self._address)
            logger.exception(msg)
            raise TTransportException(TTransportException.NOT_OPEN, msg)

在上述代码中,倒数第二行中使用logger输出了一个异常信息。定位到问题在此处
继续验证logger,使用thrift源码中打印日志的方式,直接打印一个异常信息字符串

>>> import logging
>>> logger = logging.getLogger(__name__)
>>> logger.exception('test')
No handlers could be found for logger "__main__"

可以看到直接使用logger会出现没有handler错误,其中__main__是logging绑定的模块
所以问题原因为thrift中打印日志没有绑定handler

3.解决

python官方logging.handler文档https://docs.python.org/2/library/logging.handlers.html#module-logging.handlers
python中使用logging打印日志需要绑定handler用于向不同的输出端打log。在thrifit中没有绑定handler故出现了No handler问题。
从源码中解决这个问题,需要在初始化logging之后绑定一个handler

...
# 源码中初始化logger
logger = logging.getLogger(__name__)
# 绑定一个空handler
logger.addHandler(logging.NullHandler())
...
    原文作者:Sihanouk
    原文地址: https://www.jianshu.com/p/9f9833dcedcd
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞