Skip to content

log

CLASS DESCRIPTION
MillisecondFormatter

A formatter for standard library 'logging' that supports '%f' wildcard in format strings.

ColoredFormatter

Formatter to add colors based on levelname, following the COLORS constant.

RESET_SEQ module-attribute

RESET_SEQ = '\x1b[0m'

COLOR_SEQ module-attribute

COLOR_SEQ = '\x1b[1;%dm'

BOLD_SEQ module-attribute

BOLD_SEQ = '\x1b[1m'

ITALIC_SEQ module-attribute

ITALIC_SEQ = '\x1b[3m'

UNDERLINE_SEQ module-attribute

UNDERLINE_SEQ = '\x1b[4m'

COLORS module-attribute

COLORS = {
    "WARNING": YELLOW,
    "INFO": GREY,
    "DEBUG": ORANGE,
    "CRITICAL": YELLOW,
    "ERROR": RED,
}

MillisecondFormatter

Bases: Formatter

A formatter for standard library 'logging' that supports '%f' wildcard in format strings.

converter class-attribute instance-attribute

converter = fromtimestamp

formatTime

formatTime(record, datefmt=None)
Source code in src/downmixer/log/__init__.py
27
28
29
30
31
32
33
34
def formatTime(self, record, datefmt=None):
    converter = self.converter(record.created)
    if datefmt:
        s = converter.strftime(datefmt)[:-3]
    else:
        t = converter.strftime("%Y-%m-%d %H:%M:%S")
        s = "%s,%03d" % (t, record.msecs)
    return s

ColoredFormatter

Bases: Formatter

Formatter to add colors based on levelname, following the COLORS constant.

format

format(record)
Source code in src/downmixer/log/__init__.py
40
41
42
43
44
45
46
47
48
49
def format(self, record):
    new_record = copy.copy(record)
    levelname = new_record.levelname

    if levelname in COLORS:
        levelname_color = (
            COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
        )
        new_record.levelname = levelname_color
    return logging.Formatter.format(self, new_record)

formatter_message

formatter_message(message, use_color=True)
Source code in src/downmixer/log/__init__.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def formatter_message(message, use_color=True):
    if use_color:
        message = (
            message.replace("$RESET", RESET_SEQ)
            .replace("$BOLD", BOLD_SEQ)
            .replace("$ITALIC", ITALIC_SEQ)
            .replace("$UNDER", UNDERLINE_SEQ)
        )
    else:
        message = (
            message.replace("$RESET", "")
            .replace("$BOLD", "")
            .replace("$ITALIC", "")
            .replace("$UNDER", "")
        )
    return message

setup_logging

setup_logging(debug: bool = False)
Source code in src/downmixer/log/__init__.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def setup_logging(debug: bool = False):
    base_format = (
        "[$BOLD%(levelname)-8s$RESET] ($BOLD%(filename)s:%(lineno)d$RESET) %(message)s"
    )
    debug_base_format = (
        "[$BOLD%(levelname)-8s$RESET] {$ITALIC%(threadName)-10s$RESET} (%(funcName)s @ "
        "$BOLD%(filename)s:%(lineno)d$RESET) %(message)s "
    )
    chosen_format = debug_base_format if debug else base_format

    colored_format = formatter_message(chosen_format)
    # file_format = formatter_message(chosen_format, False)

    config = {
        "version": 1,
        "formatters": {
            "coloredFormatter": {
                "format": colored_format,
                "style": "%",
                "validate": False,
                "class": "downmixer.log.ColoredFormatter",
            },
        },
        "handlers": {
            "console": {
                "class": "logging.StreamHandler",
                "level": "DEBUG",
                "formatter": "coloredFormatter",
                "stream": "ext://sys.stdout",
            }
        },
        "loggers": {
            "downmixer": {
                "handlers": ["console"],
                "level": "DEBUG" if debug else "INFO",
                "propagate": True,
            }
        },
        "disable_existing_loggers": True,
    }

    logging.config.dictConfig(config)

    print("Logging setup finished")