-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_client.py
32 lines (25 loc) · 927 Bytes
/
basic_client.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
import pika
import json
# RabbitMQ connection
connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
channel = connection.channel()
# Declare the queue
result = channel.queue_declare("", exclusive=True)
queue_name = result.method.queue
def callback(ch, method, properties, body):
try:
log_message = body.decode("utf-8")
level = method.routing_key.split(".")[0]
name = method.routing_key.split(".")[1]
print(f"[{level}] [{name}] {log_message}")
except Exception as e:
print(f"Error processing message: {e}")
channel.queue_bind(exchange="topic_logs", queue=queue_name, routing_key="*.*")
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
print("Waiting for logs. To exit press CTRL+C")
try:
channel.start_consuming()
except KeyboardInterrupt:
print("Stopping log consumer...")
finally:
connection.close()