This commit is contained in:
yumoqing 2024-07-09 18:25:04 +08:00
parent f618ebde56
commit f9447684e8

View File

@ -1,7 +1,8 @@
import os
import sys import sys
import logging import logging
from functools import partial from functools import partial
from appPublic.timeUtils import timestampstr
levels={ levels={
"debug":logging.DEBUG, "debug":logging.DEBUG,
"info":logging.INFO, "info":logging.INFO,
@ -15,6 +16,74 @@ 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:
@ -39,6 +108,42 @@ 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):
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: class AppLogger:
def __init__(self): def __init__(self):
self.logger = create_logger(self.__class__.__name__) self.logger = create_logger(self.__class__.__name__)