pythonに基づいてRabbiitmqシステムログコードを傍受することを実現する例


紹介する
rabbiitmqデフォルトでは7つのスイッチがあります。そのうち、amq.rabbitmq.logはシステムログのスイッチです。このログはtopicタイプで、3つのレベルがあります。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              。           。
'''
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。