TOC

Python Redis Debug

如果可以的话,使用 redis-cli monitor 命令来输出所有 Redis 命令也很方便。

有时,条件不允许,或者 Redis 需要处理其他的连接,我希望将自己代码调用的 Redis 命令输出到日志中,方便调试。

import logging
import redis

LOG_REDIS = logging.getLogger('redis')
__raw_send_packed_comand = redis.Connection.send_packed_command


def send_packed_command(self, command, check_health=True):
    LOG_REDIS.debug('redis: %r, check_health: %r', command, check_health)
    return __raw_send_packed_comand(self, command, check_health)


redis.Connection.send_packed_command = send_packed_command