# -*- coding: utf-8 -*-
import pika
import sys

# 连接参数
user_info = pika.PlainCredentials('user', 'J70e6K7BRrxrU1dO')
connection = pika.BlockingConnection(pika.ConnectionParameters(
    'rabbitmq.rabbitmq.svc.cluster.local', 5672, '/', user_info))
channel = connection.channel()

# 1. 声明一个 direct 交换机
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

# 2. 从命令行读取 routing_key（默认 info）
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello Routing!'

# 3. 发消息
channel.basic_publish(
    exchange='direct_logs',
    routing_key=severity,          # 只有绑定相同 routing_key 的队列才能收到
    body=message
)
print(f" [x] Sent {severity}:{message}")

connection.close()
