aboutsummaryrefslogtreecommitdiff
path: root/src/Logging.py
blob: 49267086ae881b19f2e4bff5f958f60defc8fbe8 (about) (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import datetime, logging, logging.handlers, os, queue, sys, time, typing
from src import utils

LEVELS = {
    "trace": logging.DEBUG-1,
    "debug": logging.DEBUG,
    "info": logging.INFO,
    "warn": logging.WARN,
    "error": logging.ERROR,
    "critical": logging.CRITICAL
}

class BitBotFormatter(logging.Formatter):
    def formatTime(self, record, datefmt=None):
        datetime_obj = datetime.datetime.fromtimestamp(record.created)
        return utils.iso8601_format(datetime_obj, milliseconds=True)

class Log(object):
    def __init__(self, to_file: bool, level: str, location: str):
        logging.addLevelName(LEVELS["trace"], "TRACE")
        self.logger = logging.getLogger(__name__)

        if not level.lower() in LEVELS:
            raise ValueError("Unknown log level '%s'" % level)
        stdout_level = LEVELS[level.lower()]

        self.logger.setLevel(LEVELS["trace"])

        formatter = BitBotFormatter(
            "%(asctime)s [%(levelname)s] %(message)s",
            "%Y-%m-%dT%H:%M:%S.%fZ")

        stdout_handler = logging.StreamHandler(sys.stdout)
        stdout_handler.setLevel(stdout_level)
        stdout_handler.setFormatter(formatter)
        self.logger.addHandler(stdout_handler)

        if to_file:
            trace_path = os.path.join(location, "trace.log")
            trace_handler = logging.handlers.TimedRotatingFileHandler(
                trace_path, when="midnight", backupCount=5)
            trace_handler.setLevel(LEVELS["trace"])
            trace_handler.setFormatter(formatter)
            self.logger.addHandler(trace_handler)

            warn_path = os.path.join(location, "warn.log")
            warn_handler = logging.FileHandler(warn_path)
            warn_handler.setLevel(LEVELS["warn"])
            warn_handler.setFormatter(formatter)
            self.logger.addHandler(warn_handler)

    def trace(self, message: str, params: typing.List, **kwargs):
        self._log(message, params, LEVELS["trace"], kwargs)
    def debug(self, message: str, params: typing.List, **kwargs):
        self._log(message, params, logging.DEBUG, kwargs)
    def info(self, message: str, params: typing.List, **kwargs):
        self._log(message, params, logging.INFO, kwargs)
    def warn(self, message: str, params: typing.List, **kwargs):
        self._log(message, params, logging.WARN, kwargs)
    def error(self, message: str, params: typing.List, **kwargs):
        self._log(message, params, logging.ERROR, kwargs)
    def critical(self, message: str, params: typing.List, **kwargs):
        self._log(message, params, logging.CRITICAL, kwargs)
    def _log(self, message: str, params: typing.List, level: int, kwargs: dict):
        self.logger.log(level, message, *params, **kwargs)