Skip to content

Commit f90768d

Browse files
committed
log commands sent to the server
add tests, too
1 parent 2dee8e9 commit f90768d

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

redis/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ def _execute_command(self, command_name, command, **options):
336336
if self.subscribed and not subscription_command:
337337
raise RedisError("Cannot issue commands other than SUBSCRIBE and "
338338
"UNSUBSCRIBE while channels are open")
339+
log.debug(repr_command(command))
339340
command = self._encode_command(command)
340341
try:
341342
self.connection.send(command, self)
@@ -1435,6 +1436,10 @@ def _execute_transaction(self, commands):
14351436
commands,
14361437
(('', ('EXEC',), ''),)
14371438
)])
1439+
log.debug("MULTI")
1440+
for command in commands:
1441+
log.debug("TRANSACTION> "+ repr_command(command[1]))
1442+
log.debug("EXEC")
14381443
self.connection.send(all_cmds, self)
14391444
# parse off the response for MULTI and all commands prior to EXEC
14401445
for i in range(len(commands)+1):
@@ -1460,6 +1465,8 @@ def _execute_transaction(self, commands):
14601465
def _execute_pipeline(self, commands):
14611466
# build up all commands into a single request to increase network perf
14621467
all_cmds = ''.join([self._encode_command(c) for _1, c, _2 in commands])
1468+
for command in commands:
1469+
log.debug("PIPELINE> " + repr_command(command[1]))
14631470
self.connection.send(all_cmds, self)
14641471
data = []
14651472
for command_name, _, options in commands:

tests/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from server_commands import ServerCommandsTestCase
2+
from server_commands import ServerCommandsTestCase, LoggingTestCase
33
from connection_pool import ConnectionPoolTestCase
44
from pipeline import PipelineTestCase
55
from lock import LockTestCase
@@ -10,4 +10,5 @@ def all_tests():
1010
suite.addTest(unittest.makeSuite(ConnectionPoolTestCase))
1111
suite.addTest(unittest.makeSuite(PipelineTestCase))
1212
suite.addTest(unittest.makeSuite(LockTestCase))
13+
suite.addTest(unittest.makeSuite(LoggingTestCase))
1314
return suite

tests/server_commands.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import datetime
44
import threading
55
import time
6+
import logging
7+
import logging.handlers
68
from distutils.version import StrictVersion
79

810
class ServerCommandsTestCase(unittest.TestCase):
@@ -1257,3 +1259,55 @@ def test_binary_lists(self):
12571259
# check that it is possible to get list content by key name
12581260
for key in mapping.keys():
12591261
self.assertEqual(self.client.lrange(key, 0, -1), list(mapping[key]))
1262+
1263+
class BufferingHandler(logging.handlers.BufferingHandler):
1264+
1265+
def __init__(self):
1266+
logging.handlers.BufferingHandler.__init__(self, None)
1267+
1268+
def shouldFlush(self, record):
1269+
return False
1270+
1271+
class LoggingTestCase(unittest.TestCase):
1272+
1273+
def get_client(self):
1274+
return redis.Redis(host='localhost', port=6379, db=9)
1275+
1276+
def setUp(self):
1277+
self.client = self.get_client()
1278+
self.client.flushdb()
1279+
1280+
self.log = logging.getLogger("redis")
1281+
self.log.setLevel(logging.DEBUG)
1282+
self.handler = BufferingHandler()
1283+
self.log.addHandler(self.handler)
1284+
self.buffer = self.handler.buffer
1285+
1286+
def tearDown(self):
1287+
self.client.flushdb()
1288+
for c in self.client.connection_pool.get_all_connections():
1289+
c.disconnect()
1290+
1291+
def test_command_logging(self):
1292+
self.client.get("foo")
1293+
1294+
self.assertEqual(len(self.buffer), 1)
1295+
self.assertEqual(self.buffer[0].msg, "GET 'foo'")
1296+
1297+
def test_command_logging_pipeline(self):
1298+
pipe = self.client.pipeline(transaction=False)
1299+
pipe.get("foo")
1300+
pipe.execute()
1301+
1302+
self.assertEqual(len(self.buffer), 1)
1303+
self.assertEqual(self.buffer[0].msg, "PIPELINE> GET 'foo'")
1304+
1305+
def test_command_logging_transaction(self):
1306+
txn = self.client.pipeline(transaction=True)
1307+
txn.get("foo")
1308+
txn.execute()
1309+
1310+
self.assertEqual(len(self.buffer), 3)
1311+
messages = [x.msg for x in self.buffer]
1312+
self.assertEqual(messages,
1313+
["MULTI", "TRANSACTION> GET 'foo'", "EXEC"])

0 commit comments

Comments
 (0)