Commit ee1025c8 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Implmemted rabbitmq logging for user

parent adbe068c
import logging
import pika
from python_logging_rabbitmq import RabbitMQHandler
import json
class DRIPLoggingHandler(RabbitMQHandler):
def open_connection(self):
self.sequenceNumber = 0
"""
Connect to RabbitMQ.
"""
# Set logger for pika.
# See if something went wrong connecting to RabbitMQ.
handler = logging.StreamHandler()
handler.setFormatter(self.formatter)
rabbitmq_logger = logging.getLogger('pika')
rabbitmq_logger.addHandler(handler)
rabbitmq_logger.propagate = False
rabbitmq_logger.setLevel(logging.WARNING)
if not self.connection or self.connection.is_closed:
self.connection = pika.BlockingConnection(pika.ConnectionParameters( ** self.connection_params))
if not self.channel or self.channel.is_closed:
self.channel = self.connection.channel()
self.channel.queue_declare(queue='log_qeue_user', durable=True)
# Manually remove logger to avoid shutdown message.
rabbitmq_logger.removeHandler(handler)
def emit(self, record):
self.acquire()
try:
if not self.connection or self.connection.is_closed or not self.channel or self.channel.is_closed:
self.open_connection()
self.channel.basic_publish(
exchange='',
routing_key='log_qeue_user',
body=self.format(record),
properties=pika.BasicProperties(
delivery_mode=2)
)
except Exception:
self.channel, self.connection = None, None
self.handleError(record)
finally:
if self.close_after_emit:
self.close_connection()
self.release()
def format(self, record):
drip_record = {}
drip_record['timestamp'] = record.created
drip_record['owner'] = 'user'
drip_record['level'] = record.levelname
drip_record['loggerName'] = record.module
drip_record['message'] = record.message
drip_record['millis'] = record.created
self.sequenceNumber += 1
drip_record['sequenceNumber'] = self.sequenceNumber
drip_record['sourceClassName'] = record.module
drip_record['sourceMethodName'] = record.funcName
return json.dumps(drip_record)
\ No newline at end of file
import logging
class DRIPLogHandler(logging.StreamHandler):
def emit(self, record):
try:
msg = self.format(record)
stream = self.stream
same_line = hasattr(record, 'same_line')
if self.on_same_line and not same_line:
stream.write(self.terminator)
stream.write(msg)
if same_line:
stream.write('... ')
self.on_same_line = True
else:
stream.write(self.terminator)
self.on_same_line = False
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
\ No newline at end of file
...@@ -12,6 +12,7 @@ from planner.dum_planner import * ...@@ -12,6 +12,7 @@ from planner.dum_planner import *
import sys import sys
import tempfile import tempfile
import time import time
from drip_logging.drip_logging_handler import *
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if not getattr(logger, 'handler_set', None): if not getattr(logger, 'handler_set', None):
...@@ -31,6 +32,9 @@ def init_chanel(args): ...@@ -31,6 +32,9 @@ def init_chanel(args):
else: else:
rabbitmq_host = '127.0.0.1' rabbitmq_host = '127.0.0.1'
rabbit = DRIPLoggingHandler(host=rabbitmq_host, port=5672,exchange="")
logger.addHandler(rabbit)
connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitmq_host)) connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitmq_host))
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue=queue_name) channel.queue_declare(queue=queue_name)
......
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