ActiveMQ 消息总线日志处理程序 python

ActiveMQ message bus logging handler python

我想在 LOG.debugLOG.info,

时将消息发布到 ActiveMQ

我必须将处理程序添加到 logging

是否有任何其他 pythonic 方法可以做到这一点?

我创建了新句柄来处理这个问题

import json
import logging

from stompest.config import StompConfig
from stompest.sync import Stomp

class Handler(logging.Handler):
    def __init__(self, amq_uri, out_queue):
        logging.Handler.__init__(self)
        self.queue = queue
        self.uri = uri

    def emit(self, record):
        msg = self.format(record)
        cfg = StompConfig(self.uri)
        data = json.dumps(msg)
        client = Stomp(cfg)
        client.connect()

        try:
            client.send(self.queue, data)
        except Exception, exc:
            print "Error: ", exc
        client.disconnect()

def get_logger(uri, queue):
    logger = logging.getLogger('testlogger')
    logger.addHandler(Handler(uri, queue))