fix bug for new instance AppLogger will open a new logfile

This commit is contained in:
yumoqing 2023-12-18 22:08:30 +08:00
parent aeeaaa6e36
commit f80f461a23

View File

@ -11,20 +11,23 @@ levels={
}
defaultfmt = '%(asctime)s[%(name)s][%(levelname)s][%(filename)s:%(lineno)s]%(message)s'
logfile = -1
logger = None
g_levelname='info'
level = levels.get('info')
def create_logger(name, formater=defaultfmt, levelname=None, file=None):
global logfile, level, g_levelname
global logger, logfile, level, g_levelname
if logfile == -1:
logfile = file
ret = logging.getLogger(name)
if logger:
return logger
logger = logging.getLogger(name)
if levelname:
g_levelname = levelname
else:
levelname = g_levelname
level = levels.get(levelname, levels.get('info'))
ret.setLevel(level)
logger.setLevel(level)
format = logging.Formatter(formater)
file_handler = None
if logfile is not None:
@ -33,8 +36,8 @@ def create_logger(name, formater=defaultfmt, levelname=None, file=None):
file_handler = logging.StreamHandler()
file_handler.setFormatter(format)
ret.addHandler(file_handler)
return ret
logger.addHandler(file_handler)
return logger
class AppLogger:
def __init__(self):