Skip to content

archaeo_super_prompt.config.debug_log

[docs] module archaeo_super_prompt.config.debug_log

 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
"""Logging management module."""
import logging

COLOR_RESET = "\033[0m"
COLOR_BLUE = "\033[94m"  # Blue
COLOR_GREEN = "\033[92m"  # Green
COLOR_YELLOW = "\033[0;33m"  # Yellow


class ColorFormatter(logging.Formatter):
    level_colors = {
        logging.DEBUG: COLOR_BLUE,
        logging.INFO: COLOR_GREEN,
        logging.WARNING: COLOR_YELLOW,
    }

    def format(self, record):
        level_color = ColorFormatter.level_colors.get(
            record.levelno, COLOR_RESET
        )
        record.msg = f"{level_color}{record.msg}{COLOR_RESET}"
        return super().format(record)


handler = logging.StreamHandler()
handler.setFormatter(ColorFormatter("%(levelname)s: %(message)s"))

# Configure logger
logger = logging.getLogger(__name__)
# TODO: make a production mode with logging.INFO mode
# logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.propagate = False  # Avoid duplicate logs


def set_debug_mode(debug_mode: bool):
    logger.setLevel(logging.DEBUG if debug_mode else logging.INFO)


def print_log(msg: str):
    logger.info(msg)


def print_warning(msg: str):
    logger.warning(msg)


def forward_warning(error: Exception):
    logger.warning("Exception during runtime:", exc_info=error)


def print_debug_log(msg: str):
    logger.debug(msg)