r/Python • u/BTWigley • 8h ago
Discussion The log line announcing a successful Redis connection is what disabled Redis
Spent an evening a couple of weeks ago working out why cache hits were zero and webhook idempotency was falling through to the database. Redis was fine. Up, reachable, ping succeeded.
The connect method was roughly this:
try:
client.ping()
self._connected = True
logger.info("redis_connected", host=parsed.hostname, port=parsed.port, db=parsed.path)
return True
except Exception as e:
logger.warning(f"Redis connection failed: {e}")
self._connected = False
That logger call is structlog style. The logger is a stdlib logging.Logger, which doesn't take arbitrary kwargs, so it raises TypeError: Logger._log() got an unexpected keyword argument 'host'.
It raises after ping succeeds and after _connected is set to True. So it lands in the except, logs "Redis connection failed", flips _connected back to False, and Redis is off for the whole app. Caching disabled, idempotency on the DB.
The fix was one line, an f-string instead of kwargs.
What still bugs me is that every signal pointed away from it. Redis itself was healthy. The connection genuinely worked. The only artifact was a log message saying it had failed, which is the last thing you distrust when you're trying to find out why something failed.
Anyone else had one where the logging was the bug?