forked from CUBigDataClass/newsbuff-datafetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitlogsBackend.py
69 lines (48 loc) · 2.47 KB
/
emitlogsBackend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/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"
errorKey = f"{platform.node()}.logs.error"
debugKey = f"{platform.node()}.logs.debug"
# reusable object to fetch the channel details and rabbitMQ details
def fetchConnection():
rabbitMQUri = os.getenv("RABBITMQ_URI")
print("Connected to RabbitMQ")
rabbitMQ = pika.BlockingConnection(
pika.URLParameters(rabbitMQUri))
rabbitMQChannel = rabbitMQ.channel()
rabbitMQChannel.exchange_declare(exchange='backendlogs', 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='backendlogs', 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='backendlogs', 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='backendlogs', routing_key=key, body=message)
# reusable object to publish "error message" into the logs.
# Takes two parameters: <error message to be pusblished> and <rabbitMQChannel object received from fetchConnection() call>
def log_error(message, rabbitMQChannel, key=errorKey):
print("ERROR:", message, file=sys.stdout)
rabbitMQChannel.basic_publish(
exchange='backendlogs', 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()