Commit ce2b1230 authored by Federico Sismondi's avatar Federico Sismondi
Browse files

enhancements on amqp connection for logger;

parent 0e8f1af5
......@@ -33,27 +33,29 @@ import time
import os
from inspect import istraceback
#Support order in python 2.7 and 3
# Support order in python 2.7 and 3
try:
from collections import OrderedDict
except ImportError:
pass
VERSION = '0.0.3'
VERSION = '0.0.4'
# defaults vars
AMQP_URL = 'amqp://guest:guest@localhost'
AMQP_EXCHANGE = 'amq.topic'
# import AMQP variables from environment
try:
AMQP_URL = str(os.environ['AMQP_URL'])
AMQP_EXCHANGE = str(os.environ['AMQP_EXCHANGE'])
print('Env vars for AMQP connection succesfully imported')
print('URL: %s'%AMQP_URL)
print('URL: %s' % AMQP_URL)
print('AMQP_EXCHANGE: %s' % AMQP_EXCHANGE)
except KeyError as e:
print(' Cannot retrieve environment variables for AMQP connection, using default url: %s, exchange: %s'%(AMQP_URL,AMQP_EXCHANGE))
print(' Cannot retrieve environment variables for AMQP connection, using default url: %s, exchange: %s' % (
AMQP_URL, AMQP_EXCHANGE))
# skip natural LogRecord attributes
# http://docs.python.org/library/logging.html#logrecord-attributes
......@@ -74,7 +76,7 @@ def merge_record_extra(record, target, reserved=RESERVED_ATTR_HASH):
:param reserved: dict or list with reserved keys to skip
"""
for key, value in record.__dict__.items():
#this allows to have numeric keys
# this allows to have numeric keys
if (key not in reserved
and not (hasattr(key, "startswith")
and key.startswith('_'))):
......@@ -100,7 +102,7 @@ class JsonFormatter(logging.Formatter):
self.json_default = kwargs.pop("json_default", None)
self.json_encoder = kwargs.pop("json_encoder", None)
self.prefix = kwargs.pop("prefix", "")
#super(JsonFormatter, self).__init__(*args, **kwargs)
# super(JsonFormatter, self).__init__(*args, **kwargs)
logging.Formatter.__init__(self, *args, **kwargs)
if not self.json_encoder and not self.json_default:
def _default_json_handler(obj):
......@@ -113,6 +115,7 @@ class JsonFormatter(logging.Formatter):
elif isinstance(obj, Exception):
return "Exception: %s" % str(obj)
return str(obj)
self.json_default = _default_json_handler
self._required_fields = self.parse()
self._skip_fields = dict(zip(self._required_fields,
......@@ -184,6 +187,7 @@ class RabbitMQHandler(logging.Handler):
Example setup::
handler = RabbitMQHandler('amqp://guest:guest@localhost')
"""
def __init__(self, url, name, exchange=AMQP_EXCHANGE):
logging.Handler.__init__(self)
self.connection = pika.BlockingConnection(pika.URLParameters(url))
......@@ -193,14 +197,29 @@ class RabbitMQHandler(logging.Handler):
def emit(self, record):
routing_key = ".".join(["log", record.levelname.lower(), self.name])
self.channel.basic_publish(
try:
self.channel.basic_publish(
exchange=self.exchange,
routing_key=routing_key,
body=self.format(record),
properties = pika.BasicProperties(
content_type='application/json',
properties=pika.BasicProperties(
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()
self.channel.basic_publish(
exchange=self.exchange,
routing_key=routing_key,
body=self.format(record),
properties=pika.BasicProperties(
content_type='application/json'
)
)
def close(self):
self.channel.close()
......@@ -228,4 +247,4 @@ if __name__ == "__main__":
time.sleep(1)
logger.info("This is an info")
time.sleep(1)
logger.debug("This is a debug")
\ No newline at end of file
logger.debug("This is a debug")
Supports Markdown
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