Skip to content

Commit

Permalink
Implemented a RabbitMQHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
vals committed Oct 5, 2012
1 parent e03e6c7 commit a4678a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion logbook/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ class LoggerGroup(object):
def __init__(self, loggers=None, level=NOTSET, processor=None):
#: a list of all loggers on the logger group. Use the
#: :meth:`add_logger` and :meth:`remove_logger` methods to add
#: or remove loggers from this list.
#: or remove loggers from this list.
self.loggers = []
if loggers is not None:
for logger in loggers:
Expand Down
47 changes: 46 additions & 1 deletion logbook/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,50 @@
from logbook.helpers import json


class RabbitMQHandler(Handler):
"""A handler that acts as a RabbitMQ publisher, which publishes each record
as json dump. Requires the kombu module.
The queue will be filled with JSON exported log records. To receive such
log records from a queue you can use the :class:`RabbitMQSubscriber`.
Example setup::
handler = RabbitMQHandler('amqp://127.0.0.1', exchange='logging',
queue='my_application')
"""
def __init__(self, uri=None, exchange='logging', queue='log', level=NOTSET,
filter=None, bubble=False, context=None):
Handler.__init__(self, level, filter, bubble)
try:
import kombu
except ImportError:
raise RuntimeError('The kombu library is required for '
'the RabbitMQHandler.')

self.exchange = kombu.Exchange(exchange, 'direct', durable=True)
self.queue = kombu.Queue(queue, exchange=self.exchange, routing_key=queue)

if uri:
self.connection = kombu.Connection(uri)

def export_record(self, record):
"""Exports the record into a dictionary ready for JSON dumping.
"""
return record.to_dict(json_safe=True)

def emit(self, record):
with self.connection.Producer(serializer='json') as producer:
producer.publish(self.export_record(record), \
exchange=self.exchange, \
routing_key=self.queue.routing_key, \
declare=[self.queue])

def close(self):
self.connection.close()


class ZeroMQHandler(Handler):
"""A handler that acts as a ZeroMQ publisher, which publishes each record
as json dump. Requires the pyzmq library.
Expand Down Expand Up @@ -214,7 +258,8 @@ def _fix_261_mplog():
module is not imported by logging and as such the test in
the util fails.
"""
import logging, multiprocessing
import logging
import multiprocessing
logging.multiprocessing = multiprocessing


Expand Down

0 comments on commit a4678a3

Please sign in to comment.