Skip to content

Instantly share code, notes, and snippets.

@dpwiz
Created October 18, 2012 13:24
Show Gist options
  • Save dpwiz/3911791 to your computer and use it in GitHub Desktop.
Save dpwiz/3911791 to your computer and use it in GitHub Desktop.
log all stuff to rabbitmq
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)
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