-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
31 lines (23 loc) · 908 Bytes
/
log.py
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
import builtins
import logging
import os
from rich.logging import RichHandler
import config
def init_logging(debug=True):
log_path = f"{config.LOG_PREFIX}{os.getpid()}.log"
# Overriding default print in 3.13 breaks builtin color exception text
# use `rprint` for formatted print
builtins.print = print
logging_level = logging.DEBUG if debug else logging.WARNING
logger_config = {
"handlers": [RichHandler(rich_tracebacks=False, tracebacks_show_locals=True)], # type: ignore
"format": "PID(%(process)d) - %(message)s",
"level": logging_level,
}
logging.basicConfig(**logger_config)
# instantiate logger
logger = logging.getLogger()
if config.LOG_TO_FILES:
handler = logging.FileHandler(log_path, mode="w")
logger.addHandler(handler)
logger.info(f"Logging initialized at {logging_level=} for {os.getpid()=} ...")