bot/models/logger.py
2026-08-30 13:48:59 +03:00

66 lines
No EOL
2.4 KiB
Python

import datetime
class Color:
def __init__(self, force_unsupport = False) -> None:
try:
import colorama
colorama.init()
from colorama import Fore as C
self.support = True
self.color = C
except ImportError:
self.support = False
if force_unsupport: self.support = False
def red(self, light = True):
if not light: return self.color.RED if self.support else ''
else: return self.color.LIGHTRED_EX if self.support else ''
def yellow(self, light = True):
if not light: return self.color.YELLOW if self.support else ''
else: return self.color.LIGHTYELLOW_EX if self.support else ''
def green(self, light = True):
if not light: return self.color.GREEN if self.support else ''
else: return self.color.LIGHTGREEN_EX if self.support else ''
def cyan(self, light = True):
if not light: return self.color.CYAN if self.support else ''
else: return self.color.LIGHTCYAN_EX if self.support else ''
def blue(self, light = True):
if not light: return self.color.BLUE if self.support else ''
else: return self.color.LIGHTBLUE_EX if self.support else ''
def magenta(self, light = True):
if not light: return self.color.MAGENTA if self.support else ''
else: return self.color.LIGHTMAGENTA_EX if self.support else ''
def gray(self):
return self.color.LIGHTBLACK_EX if self.support else ''
def reset(self):
return self.color.RESET if self.support else ''
color = Color()
def _commit(type: str, object_color, runtime: str, text: str, flush: bool = False):
date = datetime.datetime.now()
log_string = f'{object_color}{type} {color.gray()}{date.strftime("%d.%m.%Y %H:%M:%S")} {color.reset()}[{runtime}] {object_color}{text}{color.red()}'
if flush: log_string += '\r'
print(log_string, end = '' if flush else '\n', flush=flush)
def log(runtime: str, text: str, flush: bool = False):
_commit('L', color.reset(), runtime, text, flush)
def success(runtime: str, text: str, flush: bool = False):
_commit('S', color.green(), runtime, text, flush)
def warn(runtime: str, text: str, flush: bool = False):
_commit('W', color.yellow(), runtime, text, flush)
def error(runtime: str, text: str, flush: bool = False):
_commit('E', color.red(), runtime, text, flush)