1. 程式人生 > 實用技巧 >監聽Rabbitmq系統日誌(python版)

監聽Rabbitmq系統日誌(python版)

介紹

rabbitmq預設有7個交換機,其中amq.rabbitmq.log為系統日誌的交換機,這個日誌為topic型別,會有三個等級的(routing_key)的日誌傳送到這個交換機上。

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import pika
# ########################### 訂閱者 ###########################
credentials = pika.PlainCredentials("使用者名稱","密碼")
connection = pika.BlockingConnection(pika.ConnectionParameters(
    
'ip', 5672, '/', credentials=credentials)) channel = connection.channel() # 宣告佇列 channel.queue_declare(queue='info_queue',durable=True) channel.queue_declare(queue='error_queue',durable=True) channel.queue_declare(queue='warning_queue',durable=True) # 繫結 channel.queue_bind(exchange='amq.rabbitmq.log
',queue="info_queue",routing_key="info") channel.queue_bind(exchange='amq.rabbitmq.log',queue="error_queue",routing_key="error") channel.queue_bind(exchange='amq.rabbitmq.log',queue="warning_queue",routing_key="warning") print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body):
print(" [x] %r" % body) print(" [x] Done") ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_consume("info_queue",callback,auto_ack=False) channel.basic_consume("error_queue",callback,auto_ack=False) channel.basic_consume("warning_queue",callback,auto_ack=False) channel.start_consuming() ''' 然後釋出者只需要給exchange傳送訊息,然後exchange繫結的多個佇列都有這個訊息了。訂閱者就收到這個訊息了。 '''