vklochan/python-logstash

No Error handling or message for wrong Port

yuseferi opened this issue · 2 comments

My Logstasah is listening TCP on 5000, and when trying you sample

import logging
import logstash
import sys

host = 'localhost'

test_logger = logging.getLogger('python-logstash-logger')
test_logger.setLevel(logging.INFO)
test_logger.addHandler(logstash.LogstashHandler(host, 5959, version=1))
# test_logger.addHandler(logstash.TCPLogstashHandler(host, 5959, version=1))

test_logger.error('python-logstash: test logstash error message.')
test_logger.info('python-logstash: test logstash info message.')
test_logger.warning('python-logstash: test logstash warning message.')

# add extra field to logstash message
extra = {
    'test_string': 'python version: ' + repr(sys.version_info),
    'test_boolean': True,
    'test_dict': {'a': 1, 'b': 'c'},
    'test_float': 1.23,
    'test_integer': 123,
    'test_list': [1, 2, '3'],
}
test_logger.info('python-logstash: test extra fields', extra=extra)

Nothing Happened , no error , nothing
Even if I changed the port to 500 still nothing.

but simple nc localhost 5000 < /var/log/test.log
it works.

The problem isn't from TCPLogstashHandler but from python's SocketHandler class.

I kind of ugly-ly workaround this problem with this:

class Logstash(logstash.TCPLogstashHandler):

    def __init__(self, *args, **kwargs):
        super(Logstash, self).__init__(*args, **kwargs)
        self._raw_log = logging.getLogger('logstash_stdout')

    def createSocket(self):
        # Force error to be raised and trigger handleError
        self.makeSocket()
        super(GoodLogstash, self).createSocket()

    def handleError(self, record):
        if settings.LOGSTASH_SHOW_ERROR:
            super(Logstash, self).handleError(record)
            self._raw_log.error('Cannot log to logstash')

The stacktrace is HUGE but at least you get something.

I don't care about having tons of logs for a single connection error as long as i have the information, so of course feel free to modify this example to add an "antispam" ;)

First of all, in the described example, a UDP Handler is used, not a TCP Handler.
As we know a UDP protocol just doesn't care about a receiver.
And the second one, it looks like the problem is resolved by using the handleError.

Please open the issue if you need more help with it.