2023-10-18 16:29:51 +00:00
|
|
|
import inspect
|
2023-09-25 14:28:32 +00:00
|
|
|
import logging
|
2023-11-29 11:40:48 +00:00
|
|
|
from collections.abc import Callable
|
2023-10-17 21:49:16 +00:00
|
|
|
from pathlib import Path
|
2023-11-29 11:40:48 +00:00
|
|
|
from typing import Any
|
2023-09-25 14:28:32 +00:00
|
|
|
|
2023-09-27 09:35:43 +00:00
|
|
|
grey = "\x1b[38;20m"
|
|
|
|
yellow = "\x1b[33;20m"
|
|
|
|
red = "\x1b[31;20m"
|
|
|
|
bold_red = "\x1b[31;1m"
|
|
|
|
green = "\u001b[32m"
|
|
|
|
blue = "\u001b[34m"
|
|
|
|
|
|
|
|
|
2023-10-18 16:29:10 +00:00
|
|
|
def get_formatter(color: str) -> Callable[[logging.LogRecord, bool], logging.Formatter]:
|
2023-10-18 16:29:51 +00:00
|
|
|
def myformatter(
|
|
|
|
record: logging.LogRecord, with_location: bool
|
|
|
|
) -> logging.Formatter:
|
2023-10-17 21:49:16 +00:00
|
|
|
reset = "\x1b[0m"
|
2024-02-21 09:20:01 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
filepath = Path(record.pathname).resolve()
|
|
|
|
filepath = Path("~", filepath.relative_to(Path.home()))
|
|
|
|
except Exception:
|
|
|
|
filepath = Path(record.pathname)
|
2024-02-21 09:16:58 +00:00
|
|
|
|
2023-10-18 16:29:10 +00:00
|
|
|
if not with_location:
|
2023-10-18 16:29:51 +00:00
|
|
|
return logging.Formatter(f"{color}%(levelname)s{reset}: %(message)s")
|
2023-10-18 16:29:10 +00:00
|
|
|
|
2023-10-17 21:49:16 +00:00
|
|
|
return logging.Formatter(
|
2024-02-21 09:16:58 +00:00
|
|
|
f"{color}%(levelname)s{reset}: %(message)s\nLocation: {filepath}:%(lineno)d::%(funcName)s\n"
|
2023-10-17 21:49:16 +00:00
|
|
|
)
|
2023-10-18 16:29:51 +00:00
|
|
|
|
2023-10-17 21:49:16 +00:00
|
|
|
return myformatter
|
2023-09-22 16:34:43 +00:00
|
|
|
|
|
|
|
|
2023-09-27 09:35:43 +00:00
|
|
|
FORMATTER = {
|
|
|
|
logging.DEBUG: get_formatter(blue),
|
|
|
|
logging.INFO: get_formatter(green),
|
|
|
|
logging.WARNING: get_formatter(yellow),
|
|
|
|
logging.ERROR: get_formatter(red),
|
|
|
|
logging.CRITICAL: get_formatter(bold_red),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CustomFormatter(logging.Formatter):
|
2024-01-19 17:52:51 +00:00
|
|
|
def __init__(self, log_locations: bool) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.log_locations = log_locations
|
|
|
|
|
2023-10-17 21:49:16 +00:00
|
|
|
def format(self, record: logging.LogRecord) -> str:
|
2024-01-19 17:52:51 +00:00
|
|
|
return FORMATTER[record.levelno](record, self.log_locations).format(record)
|
2023-10-18 16:29:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ThreadFormatter(logging.Formatter):
|
|
|
|
def format(self, record: logging.LogRecord) -> str:
|
|
|
|
return FORMATTER[record.levelno](record, False).format(record)
|
|
|
|
|
2023-10-18 16:29:51 +00:00
|
|
|
|
2023-10-18 16:29:10 +00:00
|
|
|
def get_caller() -> str:
|
|
|
|
frame = inspect.currentframe()
|
|
|
|
if frame is None:
|
|
|
|
return "unknown"
|
|
|
|
caller_frame = frame.f_back
|
|
|
|
if caller_frame is None:
|
|
|
|
return "unknown"
|
|
|
|
caller_frame = caller_frame.f_back
|
|
|
|
if caller_frame is None:
|
|
|
|
return "unknown"
|
|
|
|
frame_info = inspect.getframeinfo(caller_frame)
|
2024-02-21 09:20:01 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
filepath = Path(frame_info.filename).resolve()
|
|
|
|
filepath = Path("~", filepath.relative_to(Path.home()))
|
|
|
|
except Exception:
|
|
|
|
filepath = Path(frame_info.filename)
|
2024-02-21 09:16:58 +00:00
|
|
|
|
|
|
|
ret = f"{filepath}:{frame_info.lineno}::{frame_info.function}"
|
2023-10-18 16:29:10 +00:00
|
|
|
return ret
|
2023-09-22 16:34:43 +00:00
|
|
|
|
|
|
|
|
2024-02-02 05:04:30 +00:00
|
|
|
def setup_logging(level: Any, root_log_name: str = __name__.split(".")[0]) -> None:
|
2024-01-12 15:46:44 +00:00
|
|
|
# Get the root logger and set its level
|
2024-02-02 05:04:30 +00:00
|
|
|
main_logger = logging.getLogger(root_log_name)
|
2024-01-12 15:46:44 +00:00
|
|
|
main_logger.setLevel(level)
|
|
|
|
|
|
|
|
# Create and add the default handler
|
|
|
|
default_handler = logging.StreamHandler()
|
|
|
|
|
|
|
|
# Create and add your custom handler
|
|
|
|
default_handler.setLevel(level)
|
2024-02-21 09:16:58 +00:00
|
|
|
default_handler.setFormatter(CustomFormatter(str(level) == str(logging.DEBUG)))
|
2024-01-12 15:46:44 +00:00
|
|
|
main_logger.addHandler(default_handler)
|
|
|
|
|
|
|
|
# Set logging level for other modules used by this module
|
2023-10-27 21:36:45 +00:00
|
|
|
logging.getLogger("asyncio").setLevel(logging.INFO)
|
|
|
|
logging.getLogger("httpx").setLevel(level=logging.WARNING)
|