summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Maier <willmaier@ml1.net>2011-01-06 12:46:35 -0600
committerWill Maier <willmaier@ml1.net>2011-01-06 12:46:35 -0600
commitf90768d412e0a0eda54d2ecd9a92fef78f1d331d (patch)
treeacfc42cdbde3591f5480e2e9cc1008f0bffe5428
parent2dee8e9ae6ba516ba70014030fdd6b6002061128 (diff)
downloadredis-py-f90768d412e0a0eda54d2ecd9a92fef78f1d331d.tar.gz
log commands sent to the server
add tests, too
-rw-r--r--redis/client.py7
-rw-r--r--tests/__init__.py3
-rw-r--r--tests/server_commands.py54
3 files changed, 63 insertions, 1 deletions
diff --git a/redis/client.py b/redis/client.py
index 9eeac78..89b97aa 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -336,6 +336,7 @@ class Redis(threading.local):
if self.subscribed and not subscription_command:
raise RedisError("Cannot issue commands other than SUBSCRIBE and "
"UNSUBSCRIBE while channels are open")
+ log.debug(repr_command(command))
command = self._encode_command(command)
try:
self.connection.send(command, self)
@@ -1435,6 +1436,10 @@ class Pipeline(Redis):
commands,
(('', ('EXEC',), ''),)
)])
+ log.debug("MULTI")
+ for command in commands:
+ log.debug("TRANSACTION> "+ repr_command(command[1]))
+ log.debug("EXEC")
self.connection.send(all_cmds, self)
# parse off the response for MULTI and all commands prior to EXEC
for i in range(len(commands)+1):
@@ -1460,6 +1465,8 @@ class Pipeline(Redis):
def _execute_pipeline(self, commands):
# build up all commands into a single request to increase network perf
all_cmds = ''.join([self._encode_command(c) for _1, c, _2 in commands])
+ for command in commands:
+ log.debug("PIPELINE> " + repr_command(command[1]))
self.connection.send(all_cmds, self)
data = []
for command_name, _, options in commands:
diff --git a/tests/__init__.py b/tests/__init__.py
index 8931b07..63d6741 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,5 +1,5 @@
import unittest
-from server_commands import ServerCommandsTestCase
+from server_commands import ServerCommandsTestCase, LoggingTestCase
from connection_pool import ConnectionPoolTestCase
from pipeline import PipelineTestCase
from lock import LockTestCase
@@ -10,4 +10,5 @@ def all_tests():
suite.addTest(unittest.makeSuite(ConnectionPoolTestCase))
suite.addTest(unittest.makeSuite(PipelineTestCase))
suite.addTest(unittest.makeSuite(LockTestCase))
+ suite.addTest(unittest.makeSuite(LoggingTestCase))
return suite
diff --git a/tests/server_commands.py b/tests/server_commands.py
index 2738e91..5e96dcf 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -3,6 +3,8 @@ import unittest
import datetime
import threading
import time
+import logging
+import logging.handlers
from distutils.version import StrictVersion
class ServerCommandsTestCase(unittest.TestCase):
@@ -1257,3 +1259,55 @@ class ServerCommandsTestCase(unittest.TestCase):
# check that it is possible to get list content by key name
for key in mapping.keys():
self.assertEqual(self.client.lrange(key, 0, -1), list(mapping[key]))
+
+class BufferingHandler(logging.handlers.BufferingHandler):
+
+ def __init__(self):
+ logging.handlers.BufferingHandler.__init__(self, None)
+
+ def shouldFlush(self, record):
+ return False
+
+class LoggingTestCase(unittest.TestCase):
+
+ def get_client(self):
+ return redis.Redis(host='localhost', port=6379, db=9)
+
+ def setUp(self):
+ self.client = self.get_client()
+ self.client.flushdb()
+
+ self.log = logging.getLogger("redis")
+ self.log.setLevel(logging.DEBUG)
+ self.handler = BufferingHandler()
+ self.log.addHandler(self.handler)
+ self.buffer = self.handler.buffer
+
+ def tearDown(self):
+ self.client.flushdb()
+ for c in self.client.connection_pool.get_all_connections():
+ c.disconnect()
+
+ def test_command_logging(self):
+ self.client.get("foo")
+
+ self.assertEqual(len(self.buffer), 1)
+ self.assertEqual(self.buffer[0].msg, "GET 'foo'")
+
+ def test_command_logging_pipeline(self):
+ pipe = self.client.pipeline(transaction=False)
+ pipe.get("foo")
+ pipe.execute()
+
+ self.assertEqual(len(self.buffer), 1)
+ self.assertEqual(self.buffer[0].msg, "PIPELINE> GET 'foo'")
+
+ def test_command_logging_transaction(self):
+ txn = self.client.pipeline(transaction=True)
+ txn.get("foo")
+ txn.execute()
+
+ self.assertEqual(len(self.buffer), 3)
+ messages = [x.msg for x in self.buffer]
+ self.assertEqual(messages,
+ ["MULTI", "TRANSACTION> GET 'foo'", "EXEC"])