-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding reusable object to implement logging mechanism for the fronten…
…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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
||
|
||
|
||
|