Fixed global logger, renamed BaseLogger.py to Logger.py, added MultiLogger.py and changed how warnings look.
This commit is contained in:
parent
0c1cb9f214
commit
b35363e9ca
7 changed files with 73 additions and 49 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
venv/
|
||||
*.log
|
|
@ -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
|
||||
|
|
|
@ -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))
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
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))
|
||||
|
|
12
src/lib/MultiLogger.py
Normal file
12
src/lib/MultiLogger.py
Normal file
|
@ -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)
|
15
src/test.py
15
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")
|
||||
|
|
Reference in a new issue