how to reconnect when connection lost?
xdewx opened this issue · 2 comments
xdewx commented
h = logstash.LogstashHandler(host, port, **kwargs)
logger.addHandler(h)
If restart logstash, the connection will lost. how to ensure logger to reconnect logstash as soon as possible in each log?
RomkaSk commented
Please see documentations for class SocketHandler in python/logging/handlers.py:529 namely pay attention to the methods emit() and handleError().
def emit(self, record):
"""
Emit a record.
Pickles the record and writes it to the socket in binary format.
If there is an error with the socket, silently drop the packet.
If there was a problem with the socket, re-establishes the
socket.
"""
...
def handleError(self, record):
"""
Handle an error during logging.
An error has occurred during logging. Most likely cause -
connection lost. Close the socket so that we can retry on the next event.
"""
...
If you don't want to lose packets, you can create a custom handler that extends the logstash.LogstashHandler/TCPLogstashHandler
and overrides the emit() method. Within the custom handler, you can implement the reconnection logic to handle connection failures.
Something like:
class ReconnectingLogstashHandler(logstash.LogstashHandler):
def __init__(self, host, port, **kwargs):
super().__init__(host, port, **kwargs)
def emit(self, record):
try:
if not self.sock:
self.createSocket()
super().emit(record)
except (ConnectionError, OSError):
# Connection lost, handle reconnection
self.sock = None
self.createSocket() # Create a new socket
# Wait n-seconds before re-emitting the log record
time.sleep(1)
self.emit(record)