This commit is contained in:
yumoqing 2024-07-10 13:47:45 +08:00
parent f9447684e8
commit e42f61c8a2
2 changed files with 96 additions and 68 deletions

View File

@ -16,74 +16,6 @@ logger = None
g_levelname='info' g_levelname='info'
level = levels.get('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): def create_logger(name, formater=defaultfmt, levelname=None, file=None):
global logger, logfile, level, g_levelname global logger, logfile, level, g_levelname
if logfile == -1: if logfile == -1:
@ -108,6 +40,7 @@ def create_logger(name, formater=defaultfmt, levelname=None, file=None):
logger.addHandler(file_handler) logger.addHandler(file_handler)
return logger return logger
def info(*args, **kw): def info(*args, **kw):
global logger global logger
if logger is None: if logger is None:

95
appPublic/log.py Normal file
View File

@ -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)