diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-11-21 16:51:43 +0000 |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-11-21 16:51:43 +0000 |
commit | 465f7a7f2b30f233b0cfc611a6411f68f58c2732 (patch) | |
tree | 2e9ddeac830fd714573c957f89e34f6db9aca166 /logutils | |
parent | 71908d6d93695bddee1c2a363aafbb0c530e893c (diff) | |
download | logutils-465f7a7f2b30f233b0cfc611a6411f68f58c2732.tar.gz |
Made ready for 0.3.2.0.3.2
Diffstat (limited to 'logutils')
-rw-r--r-- | logutils/__init__.py | 2 | ||||
-rw-r--r-- | logutils/queue.py | 11 | ||||
-rw-r--r-- | logutils/redis.py | 72 |
3 files changed, 83 insertions, 2 deletions
diff --git a/logutils/__init__.py b/logutils/__init__.py index 377da43..f9a677c 100644 --- a/logutils/__init__.py +++ b/logutils/__init__.py @@ -10,7 +10,7 @@ of Python, and so are packaged here. import logging from string import Template -__version__ = '0.3.1' +__version__ = '0.3.2' class NullHandler(logging.Handler): """ diff --git a/logutils/queue.py b/logutils/queue.py index 03ed570..39be637 100644 --- a/logutils/queue.py +++ b/logutils/queue.py @@ -214,6 +214,15 @@ class QueueListener(object): except queue.Empty: break + def enqueue_sentinel(self): + """ + Writes a sentinel to the queue to tell the listener to quit. This + implementation uses ``put_nowait()``. You may want to override this + method if you want to use timeouts or work with custom queue + implementations. + """ + self.queue.put_nowait(self._sentinel) + def stop(self): """ Stop the listener. @@ -223,7 +232,7 @@ class QueueListener(object): may be some records still left on the queue, which won't be processed. """ self._stop.set() - self.queue.put_nowait(self._sentinel) + self.enqueue_sentinel() self._thread.join() self._thread = None diff --git a/logutils/redis.py b/logutils/redis.py new file mode 100644 index 0000000..2d7c6bf --- /dev/null +++ b/logutils/redis.py @@ -0,0 +1,72 @@ +""" +This module contains classes which help you work with Redis queues. +""" + +from logutils.queue import QueueHandler, QueueListener +try: + import cPickle as pickle +except ImportError: + import pickle + +class RedisQueueHandler(QueueHandler): + """ + A QueueHandler implementation which pushes pickled + records to a Redis queue using a specified key. + + :param key: The key to use for the queue. Defaults to + "python.logging". + :param redis: If specified, this instance is used to + communicate with a Redis instance. + :param limit: If specified, the queue is restricted to + have only this many elements. + """ + def __init__(self, key='python.logging', redis=None, limit=0): + if redis is None: + from redis import Redis + redis = Redis() + self.key = key + assert limit >= 0 + self.limit = limit + QueueHandler.__init__(self, redis) + + def enqueue(self, record): + s = pickle.dumps(vars(record)) + self.queue.rpush(self.key, s) + if self.limit: + self.queue.ltrim(self.key, -self.limit, -1) + +class RedisQueueListener(QueueListener): + """ + A QueueListener implementation which fetches pickled + records from a Redis queue using a specified key. + + :param key: The key to use for the queue. Defaults to + "python.logging". + :param redis: If specified, this instance is used to + communicate with a Redis instance. + """ + def __init__(self, *handlers, **kwargs): + redis = kwargs.get('redis') + if redis is None: + from redis import Redis + redis = Redis() + self.key = kwargs.get('key', 'python.logging') + QueueListener.__init__(self, redis, *handlers) + + def dequeue(self, block): + """ + Dequeue and return a record. + """ + if block: + s = self.queue.blpop(self.key)[1] + else: + s = self.queue.lpop(self.key) + if not s: + record = None + else: + record = pickle.loads(s) + return record + + def enqueue_sentinel(self): + self.queue.rpush(self.key, '') + |