From 1301f1a0f1c6a50049e660ba8db50d0d07803c2e Mon Sep 17 00:00:00 2001 From: Sean-Morrison Date: Thu, 6 Apr 2023 15:31:16 -0500 Subject: [PATCH] update splog.py --- bin/splog.py | 72 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/bin/splog.py b/bin/splog.py index bed1c5d3..f616dee5 100644 --- a/bin/splog.py +++ b/bin/splog.py @@ -4,24 +4,28 @@ import os.path as ptt import inspect from os import rename +import sys + class StreamToLogger(object): """ Fake file-like stream object that redirects writes to a logger instance. """ def __init__(self, logger, level): - self.logger = logger - self.level = level - self.linebuf = '' + self.logger = logger + self.level = level + self.linebuf = '' def write(self, buf): - for line in buf.rstrip().splitlines(): - self.logger.log(self.level, line.rstrip()) + for line in buf.rstrip().splitlines(): + self.logger.log(self.level, line.rstrip()) def flush(self): pass + + def backup(logfile): if not ptt.exists(logfile): return else: @@ -50,11 +54,38 @@ def __init__(self): self.warning = self._log.warning self.error = self._log.error self.debug = self._log.critical + + self._formatter = logging.Formatter('%(funcName)s: %(message)s') + # create console handler + ch = logging.StreamHandler() + ch.setLevel(logging.DEBUG) + ch.setFormatter(self._formatter) + self._log.addHandler(ch) + - def open(self, logfile=None, logprint=False): + def exception(self, exctype, value, tb): + filename = tb.tb_frame.f_code.co_filename + name = tb.tb_frame.f_code.co_name + line_no = tb.tb_lineno + error = ('--------------------------------------------\n'+ + ' Traceback'+'\n'+ + ' Type: '+ str(exctype.__name__)+'\n'+ + ' Value: '+ str(value)+'\n'+ + ' Traceback: '+f"File {filename} line {line_no}, in {name}") + while True: + try: + tb = tb.tb_next + filename = tb.tb_frame.f_code.co_filename + name = tb.tb_frame.f_code.co_name + line_no = tb.tb_lineno + error = error+'\n '+f"File {filename} line {line_no}, in {name}" + except: + break + self._log.error(error) - formatter = logging.Formatter('%(funcName)s: %(message)s') + def open(self, logfile=None, logprint=False): + if logfile is not None: backup(logfile) @@ -62,14 +93,18 @@ def open(self, logfile=None, logprint=False): # create file handler fh = logging.FileHandler(logfile) fh.setLevel(logging.DEBUG) - fh.setFormatter(formatter) + fh.setFormatter(self._formatter) self._log.addHandler(fh) - # create console handler - ch = logging.StreamHandler() - ch.setLevel(logging.DEBUG) - ch.setFormatter(formatter) - self._log.addHandler(ch) + + # Install exception handler + self._bkexecpthook = sys.excepthook + sys.excepthook = self.exception +# # create console handler +# ch = logging.StreamHandler() +# ch.setLevel(logging.DEBUG) +# ch.setFormatter(formatter) +# self._log.addHandler(ch) if logprint is True: sys.stdout = StreamToLogger(self._log,logging.INFO) @@ -79,4 +114,15 @@ def close(self): for handler in self._log.handlers: handler.close() self._log.removeFilter(handler) + while self._log.hasHandlers(): + self._log.removeHandler(self._log.handlers[0]) + + + sys.excepthook = self._bkexecpthook + #for handler in self._log.handlers: + # handler.close() + # self._log.removeFilter(handler) +# logger = logging.getLogger() +# while logger.hasHandlers(): +# logger.removeHandler(logger.handlers[0])