summaryrefslogtreecommitdiff
path: root/src/pyssg/per_level_formatter.py
blob: 7b9389062ff68d891ab4e436c72e05af6d1f8fd3 (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
from logging import Formatter, DEBUG, INFO, WARNING, ERROR, CRITICAL


class PerLevelFormatter(Formatter):
    # colors for the terminal in ansi
    __YELLOW: str = "\x1b[33m"
    __RED: str = "\x1b[31m"
    __BOLD_RED: str = "\x1b[31;1m"
    __RESET: str = "\x1b[0m"

    __DATE_FMT: str = '%Y-%m-%d %H:%M:%S'
    __COMMON_FMT: str = '[%(levelname)s] [%(module)s:%(funcName)s:%(lineno)d]: %(message)s'
    __FORMATS: dict[int, str] = {
        DEBUG: __COMMON_FMT,
        INFO: '%(message)s',
        WARNING: f'{__YELLOW}{__COMMON_FMT}{__RESET}',
        ERROR: f'{__RED}{__COMMON_FMT}{__RESET}',
        CRITICAL: f'{__BOLD_RED}{__COMMON_FMT}{__RESET}'
    }


    def format(self, record: str) -> str:
        fmt: str = self.__FORMATS.get(record.levelno)
        formatter: Formatter = Formatter(
            fmt=fmt, datefmt=self.__DATE_FMT, style='%')

        return formatter.format(record)