summary refs log tree commit diff
path: root/dot_ipython/profile_default/startup/20_logging.py
blob: 86ef226d5edaf3c06e853fbcb9c10d1e5fbb2d19 (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
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
66
67
68
69
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
"""Set some (hopefully) sane and comfortable logging defaults."""

import logging


def _setup() -> logging.Logger:
    import logging
    import os
    import setuptools
    import glob

    # Determine log levels
    project_level = getattr(logging, os.environ.get("LOG_LEVEL", "INFO"))
    root_level = getattr(logging, os.environ.get("ROOT_LOG_LEVEL", "WARNING"))
    root_level = max(project_level, root_level)

    # Determine projects
    project_names = (
        ["__main__", "repl"]
        + [x[:-3] for x in glob.glob("*.py")]
        + setuptools.find_packages()
    )

    # Set logging levels
    logging.root.setLevel(root_level)
    for name in project_names:
        logging.getLogger(name).setLevel(project_level)

    # Configure logging format
    try:
        from colorlog import ColoredFormatter
    except ModuleNotFoundError:
        formatter = logging.Formatter(
            "\u001b[36m%(asctime)s "
            "\u001b[0m%(levelname)-8s "
            "\u001b[32m%(threadName)s "
            "\u001b[36m%(name)s "
            "\u001b[37;1m%(message)s"
            "\u001b[0m",
            datefmt="%H:%M",
        )
    else:

        class FancyFormatter(logging.Formatter):
            """Formatter that uses a different formatter for debug logs."""

            def __init__(
                self,
                formatter: logging.Formatter,
                debug_formatter: logging.Formatter,
            ) -> None:
                super().__init__()
                self.formatter = formatter
                self.debug_formatter = debug_formatter

            def format(
                self,
                record: logging.LogRecord,
            ) -> str:
                if record.levelno <= logging.DEBUG:
                    return self.debug_formatter.format(record)
                return self.formatter.format(record)

        fmt = (
            "%(cyan)s%(asctime)s "
            "%(log_color)s%(levelname)-8s "
            "%(green)s%(threadName)s "
            "%(cyan)s%(name)s "
            "%(white)s%(message)s"
        )
        formatter = FancyFormatter(
            formatter=ColoredFormatter(
                fmt=fmt,
                datefmt="%H:%M",
                reset=True,
                log_colors={
                    "DEBUG": "white",
                    "INFO": "white",
                    "WARNING": "yellow",
                    "ERROR": "red",
                    "CRITICAL": "bold_red",
                },
            ),
            debug_formatter=ColoredFormatter(
                fmt=f"%(thin)s{fmt}",
                datefmt="%H:%M",
                reset=True,
                log_colors={
                    "DEBUG": "white",
                    "INFO": "white",
                    "WARNING": "yellow",
                    "ERROR": "red",
                    "CRITICAL": "bold_red",
                },
            ),
        )

    # Create root handler with formatter
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    if not logging.root.handlers:
        logging.root.addHandler(handler)

    return logging.getLogger("repl")


logger = _setup()

del _setup

_summarise_startup()