From f9447684e82bc169caa90378a17e1a77cf704cea Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 9 Jul 2024 18:25:04 +0800 Subject: [PATCH] bugfix --- appPublic/app_logger.py | 107 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/appPublic/app_logger.py b/appPublic/app_logger.py index 492841c..0987a36 100755 --- a/appPublic/app_logger.py +++ b/appPublic/app_logger.py @@ -1,7 +1,8 @@ +import os import sys import logging from functools import partial - +from appPublic.timeUtils import timestampstr levels={ "debug":logging.DEBUG, "info":logging.INFO, @@ -15,6 +16,74 @@ 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: @@ -39,6 +108,42 @@ 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: + return + logger.info(*args, **kw) + +def debug(*args, **kw): + global logger + if logger is None: + return + logger.debug(*args, **kw) + +def warning(*args, **kw): + global logger + if logger is None: + return + logger.warning(*aegs, **kw) + +def error(*args, **kw): + global logger + if logger is None: + return + logger.error(*args, **kw) + +def critical(*args, **kw): + global logger + if logger is None: + return + logger.critical(*args, **kw) + +def exception(*args, **kw): + global logger + if logger is None: + return + logger.exception(**args, **kw) + class AppLogger: def __init__(self): self.logger = create_logger(self.__class__.__name__)