diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2011-01-10 13:21:41 -0800 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2011-01-10 13:21:41 -0800 |
commit | b25b205f8e4ac612613be9f4a3d97f9353fd242d (patch) | |
tree | c208de30ce426c8b1b71ab614b9c4b5e058145fa /redis/client/debug.py | |
parent | f26de8ec0a2ae3dc97fc7be6ce65165a1fa17ca9 (diff) | |
download | redis-py-logging.tar.gz |
split the client into two pieces -- the normal client with no logging, and a debug client with logging.logging
Diffstat (limited to 'redis/client/debug.py')
-rw-r--r-- | redis/client/debug.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/redis/client/debug.py b/redis/client/debug.py new file mode 100644 index 0000000..8241d8e --- /dev/null +++ b/redis/client/debug.py @@ -0,0 +1,66 @@ +import logging +from redis.client.base import Connection, ConnectionPool, Redis, Pipeline + +log = logging.getLogger("redis") + +def repr_command(args): + "Represents a command as a string." + command = [args[0]] + if len(args) > 1: + command.extend(repr(x) for x in args[1:]) + return ' '.join(command) + +class DebugConnection(Connection): + def _connect(self, redis_instance): + log.debug("connecting to %s:%d/%d", self.host, self.port, self.db) + super(DebugConnection, self)._connect(redis_instance) + + def _disconnect(self): + log.debug("disconnecting from %s:%d/%d", self.host, self.port, self.db) + super(DebugConnection, self)._disconnect() + + +class DebugClient(Redis): + def __init__(self, *args, **kwargs): + pool = kwargs.pop('connection_pool', None) + if not pool: + pool = ConnectionPool(connection_class=DebugConnection) + kwargs['connection_pool'] = pool + super(DebugClient, self).__init__(*args, **kwargs) + + def _execute_command(self, command_name, command, **options): + log.debug(repr_command(command)) + return super(DebugClient, self)._execute_command( + command_name, command, **options + ) + + def pipeline(self, transaction=True): + """ + Return a new pipeline object that can queue multiple commands for + later execution. ``transaction`` indicates whether all commands + should be executed atomically. Apart from multiple atomic operations, + pipelines are useful for batch loading of data as they reduce the + number of back and forth network operations between client and server. + """ + return DebugPipeline( + self.connection, + transaction, + self.encoding, + self.errors + ) + + +class DebugPipeline(Pipeline): + def _execute_transaction(self, commands): + log.debug("MULTI") + for command in commands: + log.debug("TRANSACTION> "+ repr_command(command[1])) + log.debug("EXEC") + return super(DebugPipeline, self)._execute_transaction(commands) + + def _execute_pipeline(self, commands): + for command in commands: + log.debug("PIPELINE> " + repr_command(command[1])) + return super(DebugPipeline, self)._execute_pipeline(commands) + +
\ No newline at end of file |