Renamed the console.py classes, renamed _class to _object, added names to MultiLogger.py
This commit is contained in:
parent
b35363e9ca
commit
1cce98502f
6 changed files with 114 additions and 102 deletions
|
@ -5,7 +5,7 @@ from .LogLevels import Level
|
||||||
|
|
||||||
|
|
||||||
class Log:
|
class Log:
|
||||||
def __init__(self, level: Level, text: str, *args, _class=None, _file=inspect.stack()[1].filename):
|
def __init__(self, level: Level, text: str, *args, _object=None, _file=inspect.stack()[1].filename):
|
||||||
"""
|
"""
|
||||||
Create a log message.
|
Create a log message.
|
||||||
"""
|
"""
|
||||||
|
@ -14,17 +14,17 @@ class Log:
|
||||||
self.args = args
|
self.args = args
|
||||||
self.created_on = time.time()
|
self.created_on = time.time()
|
||||||
self.file = _file
|
self.file = _file
|
||||||
self._class = _class
|
self._object = _object
|
||||||
|
|
||||||
def format_time(self, fmt: str):
|
def format_time(self, fmt: str):
|
||||||
return datetime.datetime.fromtimestamp(self.created_on).strftime(fmt)
|
return datetime.datetime.fromtimestamp(self.created_on).strftime(fmt)
|
||||||
|
|
||||||
def class_str(self):
|
def class_str(self):
|
||||||
if self._class:
|
if self._object:
|
||||||
prefix = f'{type(self._class).__name__}'
|
prefix = f'{type(self._object).__name__}'
|
||||||
if hasattr(self._class, 'name'):
|
if hasattr(self._object, 'name'):
|
||||||
prefix += ":"
|
prefix += ":"
|
||||||
prefix += self._class.name
|
prefix += self._object.name
|
||||||
return prefix
|
return prefix
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from . import console
|
from . import console
|
||||||
|
|
||||||
class Level:
|
class Level:
|
||||||
def __init__(self, name: str, weight: int, short_name: str or None = None, _color: str = console.fg.white):
|
def __init__(self, name: str, weight: int, short_name: str or None = None, _color: str = console.FG.white):
|
||||||
"""
|
"""
|
||||||
Create a logging level.
|
Create a logging level.
|
||||||
"""
|
"""
|
||||||
|
@ -15,9 +15,9 @@ class Level:
|
||||||
|
|
||||||
|
|
||||||
basic_levels = {
|
basic_levels = {
|
||||||
'FATAL': Level('FATAL', 10, _color=(console.fg.red + console.util.bold)),
|
'FATAL': Level('FATAL', 10, _color=(console.FG.red + console.Utils.bold)),
|
||||||
'ERROR': Level('ERROR', 3, _color=console.fg.red),
|
'ERROR': Level('ERROR', 3, _color=console.FG.red),
|
||||||
'WARNING': Level('WARNING', 2, _color=(console.fg.yellow + console.util.bold + console.util.underline)),
|
'WARNING': Level('WARNING', 2, _color=(console.FG.yellow + console.Utils.bold + console.Utils.underline)),
|
||||||
'INFO': Level('INFO', 1, _color=console.fg.blue),
|
'INFO': Level('INFO', 1, _color=console.FG.blue),
|
||||||
'DEBUG': Level('DEBUG', 0, _color=console.fg.green)
|
'DEBUG': Level('DEBUG', 0, _color=console.FG.green)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import sys
|
import sys
|
||||||
from .Log import Log
|
from .Log import Log
|
||||||
from .LogLevels import basic_levels, Level
|
from .LogLevels import basic_levels, Level
|
||||||
from .console import util
|
from .console import Utils
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,24 +18,24 @@ class Logger:
|
||||||
l_pre = f"{log.level.short} [{log.format_time('%Y-%m-%d %H:%M:%S')}] <{log.class_str() or log.file}>"
|
l_pre = f"{log.level.short} [{log.format_time('%Y-%m-%d %H:%M:%S')}] <{log.class_str() or log.file}>"
|
||||||
l_str = f"{log.format_text()}"
|
l_str = f"{log.format_text()}"
|
||||||
if self.do_colors:
|
if self.do_colors:
|
||||||
self._print(f"{log.level.color}{l_pre}{util.reset} {l_str}")
|
self._print(f"{log.level.color}{l_pre}{Utils.reset} {l_str}")
|
||||||
else:
|
else:
|
||||||
self._print(f'{l_pre} {l_str}')
|
self._print(f'{l_pre} {l_str}')
|
||||||
|
|
||||||
def custom(self, level: Level, text: str, *args, _class=None, _file=inspect.stack()[1].filename):
|
def custom(self, level: Level, text: str, *args, _object=None, _file=inspect.stack()[1].filename):
|
||||||
self._log(Log(level, text, *args, _class=None, _file=inspect.stack()[1].filename))
|
self._log(Log(level, text, *args, _object=None, _file=inspect.stack()[1].filename))
|
||||||
|
|
||||||
def debug(self, text: str, *args, _class=None):
|
def debug(self, text: str, *args, _object=None):
|
||||||
self._log(Log(basic_levels['DEBUG'], text, *args, _class=_class, _file=inspect.stack()[1].filename))
|
self._log(Log(basic_levels['DEBUG'], text, *args, _object=_object, _file=inspect.stack()[1].filename))
|
||||||
|
|
||||||
def info(self, text: str, *args, _class=None):
|
def info(self, text: str, *args, _object=None):
|
||||||
self._log(Log(basic_levels['INFO'], text, *args, _class=_class, _file=inspect.stack()[1].filename))
|
self._log(Log(basic_levels['INFO'], text, *args, _object=_object, _file=inspect.stack()[1].filename))
|
||||||
|
|
||||||
def warn(self, text: str, *args, _class=None):
|
def warn(self, text: str, *args, _object=None):
|
||||||
self._log(Log(basic_levels['WARNING'], text, *args, _class=_class, _file=inspect.stack()[1].filename))
|
self._log(Log(basic_levels['WARNING'], text, *args, _object=_object, _file=inspect.stack()[1].filename))
|
||||||
|
|
||||||
def err(self, text: str, *args, _class=None):
|
def err(self, text: str, *args, _object=None):
|
||||||
self._log(Log(basic_levels['ERROR'], text, *args, _class=_class, _file=inspect.stack()[1].filename))
|
self._log(Log(basic_levels['ERROR'], text, *args, _object=_object, _file=inspect.stack()[1].filename))
|
||||||
|
|
||||||
def fatal(self, text: str, *args, _class=None):
|
def fatal(self, text: str, *args, _object=None):
|
||||||
self._log(Log(basic_levels['FATAL'], text, *args, _class=_class, _file=inspect.stack()[1].filename))
|
self._log(Log(basic_levels['FATAL'], text, *args, _object=_object, _file=inspect.stack()[1].filename))
|
||||||
|
|
|
@ -3,8 +3,8 @@ from .Log import Log
|
||||||
|
|
||||||
|
|
||||||
class MultiLogger(Logger):
|
class MultiLogger(Logger):
|
||||||
def __init__(self, sub_loggers: list[Logger]):
|
def __init__(self, sub_loggers: list[Logger], _name="Generic_MultiLogger"):
|
||||||
super().__init__()
|
super().__init__(name=_name)
|
||||||
self._sub_loggers = sub_loggers
|
self._sub_loggers = sub_loggers
|
||||||
|
|
||||||
def _log(self, log: Log):
|
def _log(self, log: Log):
|
||||||
|
|
|
@ -1,93 +1,95 @@
|
||||||
from sys import stdout, stdin
|
from sys import stdout, stdin
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
class fg:
|
|
||||||
black = "\u001b[30m"
|
|
||||||
red = "\u001b[31m"
|
|
||||||
green = "\u001b[32m"
|
|
||||||
yellow = "\u001b[33m"
|
|
||||||
blue = "\u001b[34m"
|
|
||||||
magenta = "\u001b[35m"
|
|
||||||
cyan = "\u001b[36m"
|
|
||||||
white = "\u001b[37m"
|
|
||||||
|
|
||||||
def rgb(r, g, b): return f"\u001b[38;2;{r};{g};{b}m"
|
class FG:
|
||||||
|
black = "\u001b[30m"
|
||||||
|
red = "\u001b[31m"
|
||||||
|
green = "\u001b[32m"
|
||||||
|
yellow = "\u001b[33m"
|
||||||
|
blue = "\u001b[34m"
|
||||||
|
magenta = "\u001b[35m"
|
||||||
|
cyan = "\u001b[36m"
|
||||||
|
white = "\u001b[37m"
|
||||||
|
|
||||||
class bg:
|
def rgb(r, g, b): return f"\u001b[38;2;{r};{g};{b}m"
|
||||||
black = "\u001b[40m"
|
|
||||||
red = "\u001b[41m"
|
|
||||||
green = "\u001b[42m"
|
|
||||||
yellow = "\u001b[43m"
|
|
||||||
blue = "\u001b[44m"
|
|
||||||
magenta = "\u001b[45m"
|
|
||||||
cyan = "\u001b[46m"
|
|
||||||
white = "\u001b[47m"
|
|
||||||
|
|
||||||
def rgb(r, g, b): return f"\u001b[48;2;{r};{g};{b}m"
|
|
||||||
|
|
||||||
|
|
||||||
class util:
|
class BG:
|
||||||
reset = "\u001b[0m"
|
black = "\u001b[40m"
|
||||||
bold = "\u001b[1m"
|
red = "\u001b[41m"
|
||||||
underline = "\u001b[4m"
|
green = "\u001b[42m"
|
||||||
reverse = "\u001b[7m"
|
yellow = "\u001b[43m"
|
||||||
|
blue = "\u001b[44m"
|
||||||
|
magenta = "\u001b[45m"
|
||||||
|
cyan = "\u001b[46m"
|
||||||
|
white = "\u001b[47m"
|
||||||
|
|
||||||
clear = "\u001b[2J"
|
def rgb(r, g, b): return f"\u001b[48;2;{r};{g};{b}m"
|
||||||
clearline = "\u001b[2K"
|
|
||||||
|
|
||||||
up = "\u001b[1A"
|
|
||||||
down = "\u001b[1B"
|
|
||||||
right = "\u001b[1C"
|
|
||||||
left = "\u001b[1D"
|
|
||||||
|
|
||||||
nextline = "\u001b[1E"
|
class Utils:
|
||||||
prevline = "\u001b[1F"
|
reset = "\u001b[0m"
|
||||||
|
bold = "\u001b[1m"
|
||||||
|
underline = "\u001b[4m"
|
||||||
|
reverse = "\u001b[7m"
|
||||||
|
|
||||||
top = "\u001b[0;0H"
|
clear = "\u001b[2J"
|
||||||
|
clear_line = "\u001b[2K"
|
||||||
|
|
||||||
def to(x, y):
|
up = "\u001b[1A"
|
||||||
return f"\u001b[{y};{x}H"
|
down = "\u001b[1B"
|
||||||
|
right = "\u001b[1C"
|
||||||
|
left = "\u001b[1D"
|
||||||
|
|
||||||
def write(text="\n"):
|
nextline = "\u001b[1E"
|
||||||
stdout.write(text)
|
prevline = "\u001b[1F"
|
||||||
stdout.flush()
|
|
||||||
|
|
||||||
def writew(text="\n", wait=0.5):
|
top = "\u001b[0;0H"
|
||||||
for char in text:
|
|
||||||
stdout.write(char)
|
|
||||||
stdout.flush()
|
|
||||||
sleep(wait)
|
|
||||||
|
|
||||||
def read(begin=""):
|
def to(x, y):
|
||||||
text = ""
|
return f"\u001b[{y};{x}H"
|
||||||
|
|
||||||
stdout.write(begin)
|
def write(text="\n"):
|
||||||
stdout.flush()
|
stdout.write(text)
|
||||||
|
stdout.flush()
|
||||||
|
|
||||||
while True:
|
def writew(text="\n", wait=0.5):
|
||||||
char = ord(stdin.read(1))
|
for char in text:
|
||||||
|
stdout.write(char)
|
||||||
|
stdout.flush()
|
||||||
|
sleep(wait)
|
||||||
|
|
||||||
if char == 3:
|
def read(begin=""):
|
||||||
return
|
text = ""
|
||||||
elif char in (10, 13):
|
|
||||||
return text
|
|
||||||
else:
|
|
||||||
text += chr(char)
|
|
||||||
|
|
||||||
def readw(begin="", wait=0.5):
|
stdout.write(begin)
|
||||||
text = ""
|
stdout.flush()
|
||||||
|
|
||||||
for char in begin:
|
while True:
|
||||||
stdout.write(char)
|
char = ord(stdin.read(1))
|
||||||
stdout.flush()
|
|
||||||
sleep(wait)
|
|
||||||
|
|
||||||
while True:
|
if char == 3:
|
||||||
char = ord(stdin.read(1))
|
return
|
||||||
|
elif char in (10, 13):
|
||||||
|
return text
|
||||||
|
else:
|
||||||
|
text += chr(char)
|
||||||
|
|
||||||
if char == 3:
|
def readw(begin="", wait=0.5):
|
||||||
return
|
text = ""
|
||||||
elif char in (10, 13):
|
|
||||||
return text
|
for char in begin:
|
||||||
else:
|
stdout.write(char)
|
||||||
text += chr(char)
|
stdout.flush()
|
||||||
|
sleep(wait)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
char = ord(stdin.read(1))
|
||||||
|
|
||||||
|
if char == 3:
|
||||||
|
return
|
||||||
|
elif char in (10, 13):
|
||||||
|
return text
|
||||||
|
else:
|
||||||
|
text += chr(char)
|
||||||
|
|
16
src/test.py
16
src/test.py
|
@ -6,12 +6,22 @@ from lib import console
|
||||||
|
|
||||||
_LOGGER = Logger("Generic")
|
_LOGGER = Logger("Generic")
|
||||||
|
|
||||||
_LOGGER.warn("This is a simple log {}", "message.")
|
_LOGGER.info("This is a simple log {}", "message.")
|
||||||
|
|
||||||
custom_level = Level("FAULTY_LEVEL", 100, "*", (console.util.underline + console.util.bold + console.fg.cyan))
|
custom_level = Level("FAULTY_LEVEL", 100, "*", (console.Utils.underline + console.Utils.bold + console.FG.cyan))
|
||||||
_LOGGER.custom(custom_level, "ok")
|
_LOGGER.custom(custom_level, "ok")
|
||||||
|
|
||||||
_temp_logger = Logger("tmp_logger", False, open("logs.log", 'a+'))
|
_temp_logger = Logger("tmp_logger", False, open("logs.log", 'a+'))
|
||||||
_MultiLogger = MultiLogger([_LOGGER, _temp_logger])
|
_MultiLogger = MultiLogger([_LOGGER, _temp_logger], "Potato")
|
||||||
|
|
||||||
_MultiLogger.warn("OK")
|
_MultiLogger.warn("OK")
|
||||||
|
|
||||||
|
|
||||||
|
class User:
|
||||||
|
name = "faulty"
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
u = User()
|
||||||
|
|
||||||
|
_LOGGER.info("logged in.", _object=u)
|
||||||
|
|
Reference in a new issue