aboutsummaryrefslogtreecommitdiffstats
path: root/python/utils/__init__.py
blob: 3e5a0de14c9e51f53d67f231bb9465a45418ab7b (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
import logging
import logging.handlers
import os


class SerenitasFileHandler(logging.FileHandler):
    """simple class that encapsulates where we store our logs"""

    _formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )

    def __init__(self, log_file):
        if "LOG_DIR" not in os.environ:
            base_dir = os.path.expanduser("~")
        else:
            base_dir = os.getenv("LOG_DIR")
        super().__init__(filename=os.path.join(base_dir, log_file))
        self.setFormatter(SerenitasFileHandler._formatter)


class SerenitasRotatingFileHandler(logging.handlers.RotatingFileHandler):
    """simple class that encapsulates where we store our logs"""

    def __init__(self, log_file, maxBytes=0, backupCount=0):
        if "LOG_DIR" not in os.environ:
            base_dir = os.path.expanduser("~")
        else:
            base_dir = os.getenv("LOG_DIR")
        super().__init__(
            filename=os.path.join(base_dir, log_file),
            maxBytes=maxBytes,
            backupCount=backupCount,
        )
        self.setFormatter(SerenitasFileHandler._formatter)