Commit 34ec3f70 authored by Federico Sismondi's avatar Federico Sismondi

plenty of modifs

parent bc6bab5a
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -39,7 +39,7 @@ try:
except ImportError:
pass
VERSION = '0.0.4'
VERSION = '0.0.6'
# defaults vars
AMQP_URL = 'amqp://guest:guest@localhost'
......@@ -172,6 +172,7 @@ class JsonFormatter(logging.Formatter):
try:
log_record = OrderedDict()
log_record['_type'] = 'log'
log_record['component'] = record.module
except NameError:
log_record = {}
......@@ -188,14 +189,19 @@ class RabbitMQHandler(logging.Handler):
handler = RabbitMQHandler('amqp://guest:guest@localhost')
"""
def __init__(self, url, name, exchange=AMQP_EXCHANGE):
def __init__(self, url, name, exchange="amq.topic"):
logging.Handler.__init__(self)
self.connection = pika.BlockingConnection(pika.URLParameters(url))
self.url = url
self.connection = pika.BlockingConnection(pika.URLParameters(self.url))
self.channel = self.connection.channel()
self.exchange = exchange
self.name = name
self.createLock()
def emit(self, record):
self.acquire()
routing_key = ".".join(["log", record.levelname.lower(), self.name])
try:
......@@ -207,10 +213,15 @@ class RabbitMQHandler(logging.Handler):
content_type='application/json'
)
)
except pika.exceptions.ConnectionClosed:
print("Log hanlder connection closed. Reconnecting..")
self.connection = pika.BlockingConnection(pika.URLParameters(self.url))
self.channel = self.connection.channel()
# send retry
self.channel.basic_publish(
exchange=self.exchange,
routing_key=routing_key,
......@@ -220,9 +231,25 @@ class RabbitMQHandler(logging.Handler):
)
)
finally:
self.release()
def close(self):
self.channel.close()
self.acquire()
try:
if self.channel:
self.channel.close()
if self.connection:
self.connection.close()
finally:
self.release()
self.connection, self.channel = None, None
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment