commit 0c1cb9f214076466a9d70259105efb5ea30d7cdf Author: faulty Date: Fri Jul 29 02:27:43 2022 +0200 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eba74f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..bf17660 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,24 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4d29178 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b386ab5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pylogger.iml b/.idea/pylogger.iml new file mode 100644 index 0000000..0c80114 --- /dev/null +++ b/.idea/pylogger.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..8f96e54 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,11 @@ +from lib import BaseLogger +from lib import Logger + +_logger = None + + +def get_or_create_logger() -> Logger: + global _logger + if _logger is None: + _logger = Logger() + return _logger diff --git a/src/lib/BaseLogger.py b/src/lib/BaseLogger.py new file mode 100644 index 0000000..20dc40b --- /dev/null +++ b/src/lib/BaseLogger.py @@ -0,0 +1,36 @@ +from .Log import Log +from .LogLevels import basic_levels, Level +from .console import util +import inspect + + +class BaseLogger: + def __init__(self, name: str = "Generic", do_colors = True): + self.name = name + self.do_colors = do_colors + + def _log(self, log: Log): + 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()}" + if self.do_colors: + print(f"{log.level.color}{l_pre}{util.reset} {l_str}") + else: + print(f'{l_pre} {l_str}') + + def custom(self, level: Level, text: str, *args, _class=None, _file=inspect.stack()[1].filename): + self._log(Log(level, text, *args, _class=None, _file=inspect.stack()[1].filename)) + + def debug(self, text: str, *args, _class=None): + self._log(Log(basic_levels['DEBUG'], text, *args, _class=_class, _file=inspect.stack()[1].filename)) + + def info(self, text: str, *args, _class=None): + self._log(Log(basic_levels['INFO'], text, *args, _class=_class, _file=inspect.stack()[1].filename)) + + def warn(self, text: str, *args, _class=None): + self._log(Log(basic_levels['WARNING'], text, *args, _class=_class, _file=inspect.stack()[1].filename)) + + def err(self, text: str, *args, _class=None): + self._log(Log(basic_levels['ERROR'], text, *args, _class=_class, _file=inspect.stack()[1].filename)) + + def fatal(self, text: str, *args, _class=None): + self._log(Log(basic_levels['FATAL'], text, *args, _class=_class, _file=inspect.stack()[1].filename)) diff --git a/src/lib/Log.py b/src/lib/Log.py new file mode 100644 index 0000000..9673478 --- /dev/null +++ b/src/lib/Log.py @@ -0,0 +1,34 @@ +import datetime +import time +import inspect +from .LogLevels import Level + + +class Log: + def __init__(self, level: Level, text: str, *args, _class=None, _file=inspect.stack()[1].filename): + """ + Create a log message. + """ + self.level = level + self.text = text + self.args = args + self.created_on = time.time() + self.file = _file + self._class = _class + + def format_time(self, fmt: str): + return datetime.datetime.fromtimestamp(self.created_on).strftime(fmt) + + def class_str(self): + if self._class: + prefix = f'{type(self._class).__name__}' + if hasattr(self._class, 'name'): + prefix += ":" + prefix += self._class.name + return prefix + else: + return None + + def format_text(self): + return f'{self.text.format(*self.args)}' + diff --git a/src/lib/LogLevels.py b/src/lib/LogLevels.py new file mode 100644 index 0000000..01b73dc --- /dev/null +++ b/src/lib/LogLevels.py @@ -0,0 +1,23 @@ +from lib import console + +class Level: + def __init__(self, name: str, weight: int, short_name: str or None = None, _color: str = console.fg.white): + """ + Create a logging level. + """ + self.name = name + self.weight = weight + self.color = _color + if short_name is None: + self.short = name.upper()[:1] + else: + self.short = short_name.upper() + + +basic_levels = { + 'FATAL': Level('FATAL', 10, _color=(console.fg.red + console.util.bold)), + 'ERROR': Level('ERROR', 3, _color=console.fg.red), + 'WARNING': Level('WARNING', 2, _color=console.fg.yellow), + 'INFO': Level('INFO', 1, _color=console.fg.blue), + 'DEBUG': Level('DEBUG', 0, _color=console.fg.green) +} diff --git a/src/lib/Logger.py b/src/lib/Logger.py new file mode 100644 index 0000000..d68ed25 --- /dev/null +++ b/src/lib/Logger.py @@ -0,0 +1,4 @@ +from .BaseLogger import BaseLogger + +class Logger(BaseLogger): + pass \ No newline at end of file diff --git a/src/lib/console.py b/src/lib/console.py new file mode 100644 index 0000000..3153931 --- /dev/null +++ b/src/lib/console.py @@ -0,0 +1,93 @@ +from sys import stdout, stdin +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 bg: + 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: + reset = "\u001b[0m" + bold = "\u001b[1m" + underline = "\u001b[4m" + reverse = "\u001b[7m" + + clear = "\u001b[2J" + clearline = "\u001b[2K" + + up = "\u001b[1A" + down = "\u001b[1B" + right = "\u001b[1C" + left = "\u001b[1D" + + nextline = "\u001b[1E" + prevline = "\u001b[1F" + + top = "\u001b[0;0H" + + def to(x, y): + return f"\u001b[{y};{x}H" + + def write(text="\n"): + stdout.write(text) + stdout.flush() + + def writew(text="\n", wait=0.5): + for char in text: + stdout.write(char) + stdout.flush() + sleep(wait) + + def read(begin=""): + text = "" + + stdout.write(begin) + stdout.flush() + + while True: + char = ord(stdin.read(1)) + + if char == 3: + return + elif char in (10, 13): + return text + else: + text += chr(char) + + def readw(begin="", wait=0.5): + text = "" + + for char in begin: + stdout.write(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) \ No newline at end of file diff --git a/src/test.py b/src/test.py new file mode 100644 index 0000000..916caf3 --- /dev/null +++ b/src/test.py @@ -0,0 +1,8 @@ +from lib.BaseLogger import BaseLogger +from lib.LogLevels import Level +from lib.Log import Log +from lib import console + +_LOGGER = BaseLogger("Generic") + +_LOGGER.info("Started Loco {}", "v1")