1. Curtis Maloney
  2. gnocchi-tools

Commits

funkybob  committed 371ec45

Add RedisHander for logging

  • Participants
  • Parent commits 7bb1679
  • Branches default

Comments (0)

Files changed (1)

File gnocchi/tools/handler.py

View file
  • Ignore whitespace
+
+from logging import Handler
+
+import redis
+
+class RedisHandler(Handler):
+    def __init__(self, level, record_limit=1000, key='log', **kwargs):
+        self.client = redis.StrictRedis(**kwargs)
+        self.key = key
+        self.record_limit = record_limit
+        super(RedisHandler, self).__init__(level)
+
+    def emit(self, record):
+        '''
+        Serialise and emit the record to the DB
+
+        name, level, pathname, lineno, msg, args, exc_info, func=None
+        '''
+
+        # Serailise
+        data = {
+            'name': record.name,
+            'level': record.levelname,
+            'pathname': record.pathname,
+            'lineno': lineno,
+            'msg': msg,
+            'args': args,
+            'exc_info': exc_info,
+        }
+
+        # Add to Redis
+        self.client.zadd(self.key, record.created, data)
+
+        # Make sure we're not over limit
+        count = self.client.zcard(self.key)
+        if count > self.record_limit:
+            self.client.zremrangebyrank(self.key, 0, count - self.record_limit)
+