diff options
author | Frederick Muriuki Muriithi | 2024-10-24 15:49:42 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-10-24 15:49:42 -0500 |
commit | 80ae9ca56a34ae11c840bd6a7d24fbd9771c0c3e (patch) | |
tree | 44ee4fc9e92e46cff5c316a334218a74be1e3507 | |
parent | 640f9b040ca2ff3487358ef75ba676ac0a922078 (diff) | |
download | gn-uploader-80ae9ca56a34ae11c840bd6a7d24fbd9771c0c3e.tar.gz |
Add handler to send log messages to specific Redis list
Class `RedisMessageListHandler` builds a log handler that pushes any
messages logged out to the redis list specified by its
`fullyqualifiedkey` argument.
-rw-r--r-- | scripts/redis_logger.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/scripts/redis_logger.py b/scripts/redis_logger.py index 2ae682b..d3fde5f 100644 --- a/scripts/redis_logger.py +++ b/scripts/redis_logger.py @@ -1,5 +1,6 @@ """Utilities to log to redis for our worker scripts.""" import logging +from typing import Optional from redis import Redis @@ -26,6 +27,26 @@ class RedisLogger(logging.Handler): self.redisconnection.rpush(self.messageslistname, self.format(record)) self.redisconnection.expire(self.messageslistname, self.expiry) +class RedisMessageListHandler(logging.Handler): + """Send messages to specified redis list.""" + def __init__(self, + rconn: Redis, + fullyqualifiedkey: str, + loglevel: int = logging.NOTSET, + expiry: Optional[int] = 86400): + super().__init__(loglevel) + self.redisconnection = rconn + self.fullyqualifiedkey = fullyqualifiedkey + self.expiry = expiry + + def emit(self, record): + """Log out to specified `fullyqualifiedkey`.""" + self.redisconnection.rpush(self.fullyqualifiedkey, self.format(record)) + if bool(self.expiry): + self.redisconnection.expire(self.fullyqualifiedkey, self.expiry) + else: + self.redisconnection.persist(self.fullyqualifiedkey) + def setup_redis_logger(rconn: Redis, fullyqualifiedjobid: str, job_messagelist: str, |