Package: redis-logs
redis-log-handlerLog handler to forward logs to Redis.InstallationUsageHandler classesDocumentationInstallationInstallation withpip:pipinstallredis-logsUsageBasic exampleSetup log forwarding to a redis stream:fromrlhimportRedisStreamLogHandler# define your loggerlogger=logging.getLogger('my_app')# define the Redis log handlerhandler=RedisStreamLogHandler()# add the handler to the loggerlogger.addHandler(handler)After that, all the logs emitted with the logger will be forwarded to aRedis Stream; by default the logs are forwarded to a Redis instance running atlocalhost:6379in a stream namedlogs.Use a different stream namefromrlhimportRedisStreamLogHandler# define your loggerlogger=logging.getLogger('my_app')# define the Redis log handlerhandler=RedisStreamLogHandler(stream_name="custom_stream_name")# add the handler to the loggerlogger.addHandler(handler)Specify a custom Redis clientTo use a custom Redis client, you can either define your own client withredis.Redisand then pass it to the handler:fromredisimportRedisfromrlhimportRedisStreamLogHandler# define a custom Redis clientclient=Redis(host="redis",port=6380,db=1)# define your loggerlogger=logging.getLogger('my_app')# define the Redis log handler with custom Redis clienthandler=RedisStreamLogHandler(redis_client=client)# add the handler to the loggerlogger.addHandler(handler)Or dirrectly call the handler constructor with your custom Redis settings:fromrlhimportRedisStreamLogHandler# define your loggerlogger=logging.getLogger('my_app')# define the Redis log handler with custom Redis clienthandler=RedisStreamLogHandler(host="redis",port=6380,db=1)# add the handler to the loggerlogger.addHandler(handler)Specify custom log fields to saveBy default the handler only saves the logs fiedsmsg,levelnameandcreated. You can however change this default behaviour by setting your own desired fields (see the full list of fields inlogging documentation):fromrlhimportRedisStreamLogHandler# define your loggerlogger=logging.getLogger('my_app')# define the Redis log handler with custom fieldshandler=RedisStreamLogHandler(fields=["msg","name","module","levelno"])# add the handler to the loggerlogger.addHandler(handler)SaveLogRecordas pickle formatLogs can also be saved in DB aspickle format:fromrlhimportRedisStreamLogHandler# define your loggerlogger=logging.getLogger('my_app')# define the Redis log handler with as_pkl set to Truehandler=RedisStreamLogHandler(as_pkl=True)# add the handler to the loggerlogger.addHandler(handler)SaveLogRecordas pickle formatLogs can also be saved in DB as their JSON representation:fromrlhimportRedisStreamLogHandler# define your loggerlogger=logging.getLogger('my_app')# define the Redis log handler with ad_json set to Truehandler=RedisStreamLogHandler(as_json=True)# add the handler to the loggerlogger.addHandler(handler)This can be useful if you need to re-use the logs with another python program.Handlers classesCurrentlyrlhimplements two classes of handlers:RedisStreamLogHandlerRedisPubSubLogHandlerRedisStreamLogHandlerHandler used to forward logs to aRedis stream.RedisPubSubLogHandlerHandler used to publish logs to aRedis pub/subchannel.:warning: Before usingRedisPubSubLogHandler, make sure to define at least one listener to the channel, otherwise the logs emitted will be lost