aboutsummaryrefslogtreecommitdiff
path: root/scripts/redis_logger.py
blob: 76b8ba6d3deb9aacc173a57671a74f19b56af430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Utilities to log to redis for our worker scripts."""
import uuid
import logging

from redis import Redis

class RedisLogger(logging.Handler):
    """Log out to redis for our worker scripts"""
    def __init__(self, # pylint: disable=[too-many-arguments]
                 rconn: Redis,
                 jobid: uuid.UUID,
                 level:int = logging.NOTSET,
                 messagelistname: str = "log-messages",
                 expiry: int = 86400):
        """Initialise the handler."""
        super().__init__(level)
        self.redisconnection = rconn
        self.jobid = jobid
        self.messagelistname = messagelistname
        self.expiry = expiry
        rconn.hset(name=str(jobid),
                   key="log-messagelist",
                   value=self.messages_list_name())

    def messages_list_name(self):
        """Retrieve the fully qualified message-list name"""
        return f"{str(self.jobid)}:{self.messagelistname}"

    def emit(self, record):
        """Log to the messages list for a given worker."""
        self.redisconnection.rpush(
            self.messages_list_name(), self.format(record))
        self.redisconnection.expire(self.messages_list_name(), self.expiry)

def setup_redis_logger(rconn: Redis,
                       jobid: uuid.UUID,
                       job_messagelist: str,
                       expiry: int = 86400) -> RedisLogger:
    """Setup a default RedisLogger logger."""
    formatter = logging.Formatter(
                "%(asctime)s - %(name)s - %(levelname)s: %(message)s")
    # job_messagelist = f"{str(args.jobid)}:log-messages"
    rconn.hset(name=str(jobid), key="log-messagelist", value=job_messagelist)
    redislogger = RedisLogger(rconn, jobid, expiry=expiry)
    redislogger.setFormatter(formatter)
    return redislogger