Created
October 18, 2012 13:24
-
-
Save dpwiz/3911791 to your computer and use it in GitHub Desktop.
log all stuff to rabbitmq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import pika | |
from myjson import json_dumps | |
LOGGING = { | |
'version': 1, | |
'handlers': { | |
'amqp': { | |
'()': 'utils.log.PikaHandler', | |
'conn': {}, | |
'creds': ('guest', ''), | |
'exchange': 'maneuver.logs.debug', | |
} | |
}, | |
'loggers': { | |
'': { | |
'handlers': ['amqp'], | |
'level': 'DEBUG', | |
} | |
} | |
} | |
class PikaHandler(logging.Handler): | |
def __init__(self, conn={}, creds=("guest", "guest"), exchange="logs", level=logging.NOTSET): | |
logging.Handler.__init__(self, level=level) | |
self.connection = None | |
self.channel = None | |
self.pika = dict(conn, credentials=pika.PlainCredentials(*creds)) | |
self.exchange = exchange | |
def _reconnect(self): | |
if self.channel: | |
self.channel.close() | |
if self.connection: | |
self.connection.close() | |
self.connection = pika.BlockingConnection(pika.ConnectionParameters(**self.pika)) | |
self.channel = self.connection.channel() | |
self.channel.exchange_declare(exchange=self.exchange, type="topic", durable=True, auto_delete=False) | |
def emit(self, record): | |
if not(self.connection and self.channel): | |
self._reconnect() | |
routing_key = "%s.%s" % (record.name, record.levelname) | |
body = json_dumps(dict(record.__dict__, msg=record.getMessage()), default=repr) | |
self.channel.basic_publish(exchange=self.exchange, routing_key=routing_key, body=body) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pika, pprint, json | |
def callback(_c, _m, _d, msg): | |
pprint.pprint(json.loads(msg)) | |
print "---" | |
c = pika.BlockingConnection(pika.ConnectionParameters(credentials=pika.PlainCredentials('guest', ''))) | |
ch = c.channel() | |
q = ch.queue_declare(exclusive=True).method.queue | |
ch.queue_bind(exchange='maneuver.logs.production', queue=q, routing_key="#") | |
ch.basic_consume(callback, queue=q, no_ack=False) | |
ch.start_consuming() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment