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

Posted 葡萄美酒夜光杯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用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())
...

以上是关于调用thrift出现No handlers could be found for logger "thrift.transport.TSocket"的主要内容,如果未能解决你的问题,请参考以下文章

单词小站NO.276 | 考研英语大纲词汇 thrift

Oracle 监控:ORA-12519TNS:no appropriate service handler found 解决

no appropriate service handler found,修改数据库的最大连接数,默认150

IntelliJ IDEA下JavaPython实现Thrift调用

Workman-Thrift疑问解析

编译后的Thrift bin文件大