From b35363e9ca994713d483be27792d3449880aa696 Mon Sep 17 00:00:00 2001 From: faulty Date: Fri, 29 Jul 2022 03:03:38 +0200 Subject: [PATCH] Fixed global logger, renamed BaseLogger.py to Logger.py, added MultiLogger.py and changed how warnings look. --- .gitignore | 3 ++- src/__init__.py | 9 +++++---- src/lib/BaseLogger.py | 36 ----------------------------------- src/lib/LogLevels.py | 4 ++-- src/lib/Logger.py | 43 +++++++++++++++++++++++++++++++++++++++--- src/lib/MultiLogger.py | 12 ++++++++++++ src/test.py | 15 ++++++++++++--- 7 files changed, 73 insertions(+), 49 deletions(-) delete mode 100644 src/lib/BaseLogger.py create mode 100644 src/lib/MultiLogger.py diff --git a/.gitignore b/.gitignore index eba74f4..e5a42ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -venv/ \ No newline at end of file +venv/ +*.log \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py index 8f96e54..90bc100 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,11 +1,12 @@ -from lib import BaseLogger -from lib import Logger +from .lib.Logger import Logger _logger = None -def get_or_create_logger() -> Logger: +def get_or_create_logger(logger: Logger = None) -> Logger: global _logger if _logger is None: - _logger = Logger() + if not logger: + raise Exception("Global logger not found.") + _logger = logger return _logger diff --git a/src/lib/BaseLogger.py b/src/lib/BaseLogger.py deleted file mode 100644 index 20dc40b..0000000 --- a/src/lib/BaseLogger.py +++ /dev/null @@ -1,36 +0,0 @@ -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/LogLevels.py b/src/lib/LogLevels.py index 01b73dc..648206b 100644 --- a/src/lib/LogLevels.py +++ b/src/lib/LogLevels.py @@ -1,4 +1,4 @@ -from lib import console +from . import console class Level: def __init__(self, name: str, weight: int, short_name: str or None = None, _color: str = console.fg.white): @@ -17,7 +17,7 @@ class Level: 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), + 'WARNING': Level('WARNING', 2, _color=(console.fg.yellow + console.util.bold + console.util.underline)), '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 index d68ed25..9785382 100644 --- a/src/lib/Logger.py +++ b/src/lib/Logger.py @@ -1,4 +1,41 @@ -from .BaseLogger import BaseLogger +import sys +from .Log import Log +from .LogLevels import basic_levels, Level +from .console import util +import inspect -class Logger(BaseLogger): - pass \ No newline at end of file + +class Logger: + def __init__(self, name: str = "Generic", do_colors = True, output = sys.stdout): + self.name = name + self.do_colors = do_colors + self.output = output + + def _print(self, data: str, terminator: str = '\r\n'): + self.output.write(data + terminator) + + 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: + self._print(f"{log.level.color}{l_pre}{util.reset} {l_str}") + else: + self._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/MultiLogger.py b/src/lib/MultiLogger.py new file mode 100644 index 0000000..b4e9809 --- /dev/null +++ b/src/lib/MultiLogger.py @@ -0,0 +1,12 @@ +from .Logger import Logger +from .Log import Log + + +class MultiLogger(Logger): + def __init__(self, sub_loggers: list[Logger]): + super().__init__() + self._sub_loggers = sub_loggers + + def _log(self, log: Log): + for sl in self._sub_loggers: + sl._log(log) diff --git a/src/test.py b/src/test.py index 916caf3..accc4a5 100644 --- a/src/test.py +++ b/src/test.py @@ -1,8 +1,17 @@ -from lib.BaseLogger import BaseLogger +from lib.Logger import Logger +from lib.MultiLogger import MultiLogger from lib.LogLevels import Level from lib.Log import Log from lib import console -_LOGGER = BaseLogger("Generic") +_LOGGER = Logger("Generic") -_LOGGER.info("Started Loco {}", "v1") +_LOGGER.warn("This is a simple log {}", "message.") + +custom_level = Level("FAULTY_LEVEL", 100, "*", (console.util.underline + console.util.bold + console.fg.cyan)) +_LOGGER.custom(custom_level, "ok") + +_temp_logger = Logger("tmp_logger", False, open("logs.log", 'a+')) +_MultiLogger = MultiLogger([_LOGGER, _temp_logger]) + +_MultiLogger.warn("OK")