/
nightflash
/
logit
Обзор
Документация
Войти
/
nightflash
/
logit
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
logit.py
80 строк
2 KB
Гладышев Дмитрий
Update logit.py
12 ноя 2024, 16:11
12 ноя 2024, 16:11
6686c2e
Код
Авторство
О чём код?
""" Модуль для работы с логами. """ import inspect import colorama from datetime import datetime class LogIt: DEBUG = 0 INFO = 1 WARNING = 2 ERROR = 3 CRITICAL = 4 def __init__(self, use_color=True, level=1, traceall=False, traceerrors=False, timestamp=False): self.__logit_lvl = level self.__use_color = use_color self.__traceall = traceall self.__traceerrors = traceerrors self.__timestamp = timestamp if self.__use_color: colorama.init() def __log(self, msg, level): if self.__logit_lvl <= level: # если уровень лога меньше уровня сообщения if level == 0: start = colorama.Fore.LIGHTBLUE_EX end = colorama.Fore.RESET elif level == 1: start = colorama.Fore.LIGHTGREEN_EX end = colorama.Fore.RESET elif level == 2: start = colorama.Fore.LIGHTYELLOW_EX end = colorama.Fore.RESET elif level == 3: start = colorama.Fore.LIGHTRED_EX end = colorama.Fore.RESET elif level == 4: start = colorama.Fore.RED + colorama.Style.BRIGHT end = colorama.Style.RESET_ALL + colorama.Fore.RESET else: start = "" end = "" if self.__timestamp: ts = datetime.now().isoformat(timespec='microseconds') msg = ts + " " + str(msg) if self.__traceall or (self.__traceerrors and level >= 3): stk = inspect.stack()[2] # mod = inspect.getmodule(stk[0]) line = inspect.getlineno(stk[0]) fn = stk[1] func = stk[3] msg = msg + f" >> [ {fn} ].{func} line {line}" if self.__use_color: print(start + msg + end) else: print(msg) def setlevel(self, level): self.__logit_lvl = level def debug(self, msg): self.__log(msg, 0) def info(self, msg): self.__log(msg, 1) def warning(self, msg): self.__log(msg, 2) def error(self, msg): self.__log(msg, 3) def critical(self, msg): self.__log(msg, 4)