Skip to content

Commit

Permalink
Adding reusable object to implement logging mechanism for the fronten…
Browse files Browse the repository at this point in the history
…d using rabbitMQ topic exchange.
  • Loading branch information
anushab97 committed Apr 13, 2022
1 parent 5988f05 commit f3497f4
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions emitlogsFrontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

# importing all dependencies
import pika
import os
import sys
import platform


# declaring the routing keys for different topics - info/warning/debug
infoKey = f"{platform.node()}.logs.info"
warningKey = f"{platform.node()}.logs.warning"
debugKey = f"{platform.node()}.logs.debug"


# reusable object to fetch the channel details and rabbitMQ details
def fetchConnection():
rabbitMQHost = os.getenv("RABBITMQ_HOST") or "localhost"

print(f"Connecting to rabbitmq({rabbitMQHost})")

rabbitMQ = pika.BlockingConnection(
pika.ConnectionParameters(host=rabbitMQHost, heartbeat=0))
rabbitMQChannel = rabbitMQ.channel()
rabbitMQChannel.exchange_declare(exchange='frontendlogs', exchange_type='topic')
return rabbitMQChannel, rabbitMQ


# reusable object to publish "info message" into the logs.
# Takes two parameters: <info message to be pusblished> and <rabbitMQChannel object received from fetchConnection() call>
def log_info(message, rabbitMQChannel, key=infoKey):
print("INFO:", message, file=sys.stdout)
rabbitMQChannel.basic_publish(
exchange='frontendlogs', routing_key=key, body=message)

# reusable object to publish "debug message" into the logs.
# Takes two parameters: <debug message to be pusblished> and <rabbitMQChannel object received from fetchConnection() call>
def log_debug(message, rabbitMQChannel, key=debugKey):
print("DEBUG:", message, file=sys.stdout)
rabbitMQChannel.basic_publish(
exchange='frontendlogs', routing_key=key, body=message)


# reusable object to publish "warning message" into the logs.
# Takes two parameters: <warning message to be pusblished> and <rabbitMQChannel object received from fetchConnection() call>
def log_warning(message, rabbitMQChannel, key=warningKey):
print("WARNING:", message, file=sys.stdout)
rabbitMQChannel.basic_publish(
exchange='frontendlogs', routing_key=key, body=message)


# reusable object to close the rabbitMQ connection
# Takes one parameter: <rabbitMQ object received from fetchConnection() call>
def closeConnection(rabbitMQ):
rabbitMQ.close()





0 comments on commit f3497f4

Please sign in to comment.