From 2df6206461d87a015a09ff0216f4d55f8959c8f4 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Fri, 16 Dec 2022 14:12:07 -0500 Subject: [PATCH] [63] update logging --- VERSION | 2 +- modules/logs.py | 50 +++++++++++++++++---------------------------- modules/webhooks.py | 1 + 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/VERSION b/VERSION index 5239a93f..309f5637 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.18.0-develop62 +1.18.0-develop63 diff --git a/modules/logs.py b/modules/logs.py index 5e99e751..b0f80de8 100644 --- a/modules/logs.py +++ b/modules/logs.py @@ -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() diff --git a/modules/webhooks.py b/modules/webhooks.py index bacb36c0..e8536242 100644 --- a/modules/webhooks.py +++ b/modules/webhooks.py @@ -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}")