From e42f61c8a28365d00046ae51a4a000ad1df37165 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 10 Jul 2024 13:47:45 +0800 Subject: [PATCH] bugfix --- appPublic/app_logger.py | 69 +----------------------------- appPublic/log.py | 95 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 68 deletions(-) create mode 100644 appPublic/log.py diff --git a/appPublic/app_logger.py b/appPublic/app_logger.py index 0987a36..8872cd0 100755 --- a/appPublic/app_logger.py +++ b/appPublic/app_logger.py @@ -16,74 +16,6 @@ logger = None g_levelname='info' level = levels.get('info') -class MyLogger: - levels={ - "debug":6, - "info":5, - "warning":4, - "error":3, - "exception":2, - "critical":1 - } - def __init__(self, name, formater='%(timestamp)s[%(name)s][%(levelname)s][%(filename)s:%(lineno)s]%(message)s', - levelname='debug', - file=None): - self.name = name - self.formater = formater - self.levelname = levelname - self.file = file - self._create_logger() - self._handler = None - - def _create_logger(self): - self.logger = logging.getLogger(self.name) - level = self.levels.get(self.levelname, levels.get('info')) - self.logger.setLevel(level) - self.add_handler() - - def add_handler(self): - if self._handler: - self.logger.removeHandler(self._handler) - if self.file: - self._handler = logging.FileHandler(self.file) - else: - self._handler = logging.StreamHandler() - format = logging.Formatter(self.formater) - self._handler.setFormatter(self.formater) - self.logger.addHandler(self._handler) - - def check_logfile(self): - if self.file is None: - return - - if os.path.exists(self.file): - return - self.add_handler() - - def debug(self, *args, **kw): - self.check_logfile() - self.logger.debug(*args, **kw) - - def info(self, *args, **kw): - self.check_logfile() - self.logger.info(*args, **kw) - - def warning(self, *args, **kw): - self.check_logfile() - self.logger.warning(*args, **kw) - - def error(self, *args, **kw): - self.check_logfile() - self.logger.error(*args, **kw) - - def critical(self, *args, **kw): - self.check_logfile() - self.logger.critical(*args, **kw) - - def exception(self, *args, **kw): - self.check_logfile() - self.logger.exception(*args, **kw) - def create_logger(name, formater=defaultfmt, levelname=None, file=None): global logger, logfile, level, g_levelname if logfile == -1: @@ -108,6 +40,7 @@ def create_logger(name, formater=defaultfmt, levelname=None, file=None): logger.addHandler(file_handler) return logger + def info(*args, **kw): global logger if logger is None: diff --git a/appPublic/log.py b/appPublic/log.py new file mode 100644 index 0000000..c4395a7 --- /dev/null +++ b/appPublic/log.py @@ -0,0 +1,95 @@ +import sys +import codecs +from appPublic.timeUtils import timestampstr +from appPublic.Singleton import SingletonDecorator +import inspect + +def my_function(): + frame_info = inspect.currentframe() + caller_frame = frame_info.f_back + file_name = inspect.getframeinfo(caller_frame).filename + line_number = inspect.getframeinfo(caller_frame).lineno + print(f"Called from file: {file_name}, line: {line_number}") + + +@SingletonDecorator +class MyLogger: + levels={ + "debug":5, + "info":6, + "warning":4, + "error":3, + "exception":2, + "critical":1 + } + formater='%(timestamp)s[%(name)s][%(levelname)s][%(filename)s:%(lineno)s]%(message)s\n' + def __init__(self, name, levelname='debug', logfile=None): + self.name = name + self.levelname = levelname + self.level = self.levels.get(levelname) + self.logfile = logfile + + def open_logger(self): + if self.logfile: + self.logger = codecs.open(self.logfile, 'a', 'utf-8') + else: + self.logger = sys.stdout + + def close_logger(self): + if self.logfile: + self.logger.close(); + self.logger = None + self.logger = None + + def log(self, levelname, message, frame_info): + caller_frame = frame_info.f_back + filename = inspect.getframeinfo(caller_frame).filename + lineno = inspect.getframeinfo(caller_frame).lineno + level = self.levels.get(levelname) + if level > self.level: + print(f'{level=},{self.level=}') + return + data = { + 'timestamp':timestampstr(), + 'name':self.name, + 'levelname':levelname, + 'message':message, + 'filename':filename, + 'lineno':lineno + } + self.open_logger() + s = self.formater % data + self.logger.write(s) + self.logger.flush() + self.close_logger() + +def info(message): + frame_info = inspect.currentframe() + logger = MyLogger('Test') + logger.log('info', message, frame_info) + +def debug(message): + frame_info = inspect.currentframe() + logger = MyLogger('Test') + logger.log('debug', message, frame_info) + +def warning(message): + frame_info = inspect.currentframe() + logger = MyLogger('Test') + logger.log('warning', message, frame_info) + +def error(message): + frame_info = inspect.currentframe() + logger = MyLogger('Test') + logger.log('error', message, frame_info) + +def critical(message): + frame_info = inspect.currentframe() + logger = MyLogger('Test') + logger.log('critical', message, frame_info) + +def exception(message): + frame_info = inspect.currentframe() + logger = MyLogger('exception') + logger.log('exception', message, frame_info) +