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