Thrift Call Error: No handlers could be found for logger “thrift.transport.TSocket”

###1. The problem is that the thrift version is 0.10. There is no problem in 0.8. Nctagent is the structure of the thrift interface encapsulated in the code. When the thrift server is not started, it should throw an error that it cannot be connected. But before throwing the wrong stack output, there is a log exception error. This is not expected

>>> 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. The error message is thrift.transport.socket, so check the corresponding position in the source code, there is the following code

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)

In the above code, the next to last line uses logger to output an exception. Locate the problem, continue to verify the logger here, use the way of printing log in thrift source code to directly print an exception information string

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

You can see that there is no handler error when using logger directly, where__ main__ It is a module bound to logging, so the reason for the problem is that the print log in thrift is not bound to handler

###3. Solve the official Python logging.handler document https://docs.python.org/2/library/logging.handlers.html#module -Using logging to print logs in logging.handlers Python requires binding the handler to log to different outputs. There is no handler bound in thrifit, so there is no handler problem. To solve this problem from the source code, you need to bind a handler after initializing logging

...
# Initialize the logger in the source code
logger = logging.getLogger(__name__)
# Bind an empty handler
logger.addHandler(logging.NullHandler())
...

Similar Posts: