Package: redis-log-handler
redis_log_handlerHandler for the standardloggingmodule which puts logs through to Redis.InstallThis was developed against Python 3.6.7 specifically, but I don't believe it's version breaking.DevelopmentSet the local environment variableREDIS_HOSTto point to the host where your Redis runs.$ export REDIS_HOST=localhostInstall the dev requirements$ pip install -r requirements-dev.txtHow to useYou can either publish your logs to a channel,rpushthem onto a key with an optionalttlor implement the desired behaviour by deriving from the base class.To add a handler to the python logger is very simple:importloggingfromredis_log_handlerimportRedisKeyHandlerexample_handler=RedisKeyHandler('example_key')# Default parameters for Redis connection are usedlogger=logging.getLogger()# No name gives you the root loggerlogger.setLevel("WARNING")logger.addHandler(example_handler)logger.warning("This will rpush this message to the 'example_key' in Redis.")Configuring Redis ConnectionBy default each handler will create aStrictRedisinstance, passing on each argument from their__init__(**kwargs)to the StrictRedis instantiation.
This means you can configure the connection as specific as you'd like, but every argument should be provided with its keyword;Handler(host=localhost)instead ofHandler(localhost).
All available configuration options are available in tepython-redis documentation.handler=RedisKeyHandler("key",host="localhost",port=6379,password=None)connection_pool=redis.ConnectionPool(host="localhost")handler=RedisKeyHandler("key",connection_pool=connection_pool)Configure message loggingEvery handler has theraw_loggingoption which can be provided optionally.
Omitting it from the initialisation, will default it toFalse, meaning the message being logged will be purely what's sent.
If you set it toTrue, first the content will be logged, then appended to the line number and finally the pathname.pure_handler=RedisKeyHandler("key_name")raw_handler=RedisKeyHandler("other_key_name",raw_logging=True)...logging.info("Test message")Thepure_handlerwould emit a message like so:Test message.,
theraw_handlerwould emit a message like so:Test message. - 2: /.../file.py.1. RedisChannelHandlerThis opens a connection to a redis channel, allowing subscribers to pickup new messages in realtime.
Every message triggered by the logging instance, will get published to the specified channel.handler=RedisChannelHandler("channelname")2. RedisKeyHandlerThis creates/pushes onto the provided key, whatever message the logging instance will emit.
By default every message will be sent viarpush, so that when the list is retrieved usinglrange $key 0 -1, all messages are returned in the order they were sent.
Optionally attl(time to live) can be provided which will be a counter thatwill be set each timea message is sent, essentiallyrefreshing the durationof the time to live for this key.handler=RedisKeyHandler("some_key_name",ttl=60)3. Custom Redis HandlerWe also provide the ability to write custom emit functions, which get picked up by the logging instance, by inheriting the Base class.
If none of the provided Redis implementations rock you boat, simply inherit the Base class and overwrite the emit() method.In the following example we will write an example of a CustomRedisHandler which overwrites the value of the key it already exists.classCustomRedisHandler(RedisBaseHandler):def__init__(self,key:str,**kwargs:Any):super().__init__(**kwargs)self.key=keydefemit(self,message:logging.LogRecord):self.redis_client.set(self.key,str(message))