[63] update logging

This commit is contained in:
meisnate12 2022-12-16 14:12:07 -05:00
parent fe37320395
commit 2df6206461
3 changed files with 21 additions and 32 deletions

View file

@ -1 +1 @@
1.18.0-develop62
1.18.0-develop63

View file

@ -64,18 +64,20 @@ class MyLogger:
def _get_handler(self, log_file, count=3):
_handler = RotatingFileHandler(log_file, delay=True, mode="w", backupCount=count, encoding="utf-8")
self._formatter(_handler)
self._formatter(handler=_handler)
if os.path.isfile(log_file):
self._logger.removeHandler(_handler)
_handler.doRollover()
self._logger.addHandler(_handler)
return _handler
def _formatter(self, handler, border=True):
text = f"| %(message)-{self.screen_width - 2}s |" if border else f"%(message)-{self.screen_width - 2}s"
if isinstance(handler, RotatingFileHandler):
text = f"[%(asctime)s] %(filename)-27s %(levelname)-10s {text}"
handler.setFormatter(logging.Formatter(text))
def _formatter(self, handler=None, border=True, trace=False, log_only=False, space=False):
console = f"| %(message)-{self.screen_width - 2}s |" if border else f"%(message)-{self.screen_width - 2}s"
file = f"{' '*65}" if space else f"[%(asctime)s] %(filename)-27s {'[TRACE] ' if trace else '%(levelname)-10s'} "
handlers = [handler] if handler else self._logger.handlers
for h in handlers:
if not log_only or isinstance(h, RotatingFileHandler):
h.setFormatter(logging.Formatter(f"{file if isinstance(h, RotatingFileHandler) else ''}{console}"))
def add_main_handler(self):
self.main_handler = self._get_handler(self.main_log, count=9)
@ -144,20 +146,16 @@ class MyLogger:
if trace and not self.is_trace:
return None
sep = " " if space else self.separating_character
for handler in self._logger.handlers:
self._formatter(handler, border=False)
border_text = f"|{self.separating_character * self.screen_width}|"
if border:
self.print(border_text, debug=debug)
self.print(border_text, debug=debug, trace=trace)
if text:
text_list = text.split("\n")
for t in text_list:
msg = f"|{sep}{self._centered(t, sep=sep, side_space=side_space, left=left)}{sep}|"
self.print(msg, debug=debug, trace=trace)
if border:
self.print(border_text, debug=debug)
for handler in self._logger.handlers:
self._formatter(handler)
self.print(border_text, debug=debug, trace=trace)
def print(self, msg, error=False, warning=False, debug=False, trace=False):
if error:
@ -232,28 +230,20 @@ class MyLogger:
self.secrets.append(str(text))
def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False, stacklevel=1):
trace = False
original_format_str = "[%(asctime)s] %(filename)-27s %(levelname)-10s | %(message)s"
format_str = original_format_str
if level == TRACE:
trace = True
trace = level == TRACE
log_only = False
if trace:
level = DEBUG
format_str = "[%(asctime)s] %(filename)-27s [TRACE] | %(message)s"
for handler in self._logger.handlers:
if isinstance(handler, RotatingFileHandler):
handler.setFormatter(logging.Formatter(format_str))
if trace or msg.startswith("|"):
self._formatter(trace=trace, border=not msg.startswith("|"))
if self.spacing > 0:
self.exorcise()
if "\n" in msg:
for i, line in enumerate(msg.split("\n")):
self._log(level, line, args, exc_info=exc_info, extra=extra, stack_info=stack_info, stacklevel=stacklevel)
if i == 0:
for handler in self._logger.handlers:
if isinstance(handler, RotatingFileHandler):
handler.setFormatter(logging.Formatter(" " * 65 + "| %(message)s"))
for handler in self._logger.handlers:
if isinstance(handler, RotatingFileHandler):
handler.setFormatter(logging.Formatter(format_str))
self._formatter(log_only=True, space=True)
log_only = True
else:
for secret in self.secrets:
if secret in msg:
@ -275,10 +265,8 @@ class MyLogger:
exc_info = sys.exc_info()
record = self._logger.makeRecord(self._logger.name, level, fn, lno, msg, args, exc_info, func, extra, sinfo)
self._logger.handle(record)
if trace:
for handler in self._logger.handlers:
if isinstance(handler, RotatingFileHandler):
handler.setFormatter(logging.Formatter(original_format_str))
if trace or log_only or msg.startswith("|"):
self._formatter()
def findCaller(self, stack_info=False, stacklevel=1):
f = logging.currentframe()

View file

@ -15,6 +15,7 @@ class Webhooks:
self.notifiarr = notifiarr
def _request(self, webhooks, json):
logger.trace("")
logger.separator("Webhooks", space=False, border=False, trace=True)
logger.trace("")
logger.trace(f"JSON: {json}")