aboutsummaryrefslogtreecommitdiff
path: root/scripts/redis_logger.py
blob: 97ffe2624f19d559190e87cb5cd632c8c6c96888 (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
"""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)