0

I want to publish message to ActiveMQ when I do LOG.debug or LOG.info,

I have to add handler to logging.

If is there any other pythonic way to do this?

Nilesh
  • 20,521
  • 16
  • 92
  • 148

1 Answers1

1

I created new handle to handle this

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))
Nilesh
  • 20,521
  • 16
  • 92
  • 148