首页 > 解决方案 > Streaming from Python logging/stdout to a redis connection

问题描述

How can I stream Python stdout (or logging) to a redis connection?

There is a long running job that I need to capture logs for in realtime. It's not feasible to wait until the job is finished to get the logs, in case of failure there needs to be record of what happened.

I have managed to capture logging output to stdout in a StringIO buffer that can be read when the long running job is finished.

What I can't figure out is how to get that buffer to interact with the redis client in realtime, while the job is still running.

标签: pythonloggingredis

解决方案


我想到了:

from io import StringIO


class RedisHandler(StringIO):

    def __init__(self, key, redis_client):
        """
        Create a new StringIO interface for the given key and redis_client.
        """

        StringIO.__init__(self)
        self.key = key
        self.redis_client = redis_client

    def write(self, record):
        """
        Publish record to redis logging list
        """

        self.redis_client.lpush(self.key, record)

在主脚本中,我将 stdout + stderr 重定向到 RedisHandler 实例。Redis 充当 StringIO 的端点,并将日志输出中的每一行添加到列表中。


推荐阅读