apppublic/appPublic/app_logger.py
2024-07-09 18:25:04 +08:00

156 lines
3.5 KiB
Python
Executable File

import os
import sys
import logging
from functools import partial
from appPublic.timeUtils import timestampstr
levels={
"debug":logging.DEBUG,
"info":logging.INFO,
"warning":logging.WARNING,
"error":logging.error,
"critical":logging.CRITICAL
}
defaultfmt = '%(asctime)s[%(name)s][%(levelname)s][%(filename)s:%(lineno)s]%(message)s'
logfile = -1
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:
logfile = file
if logger:
return logger
logger = logging.getLogger(name)
if levelname:
g_levelname = levelname
else:
levelname = g_levelname
level = levels.get(levelname, levels.get('info'))
logger.setLevel(level)
format = logging.Formatter(formater)
file_handler = None
if logfile is not None:
file_handler = logging.FileHandler(logfile)
else:
file_handler = logging.StreamHandler()
file_handler.setFormatter(format)
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__)
self.debug = self.logger.debug
self.info = self.logger.info
self.warning = self.logger.warning
self.error = self.logger.error
self.critical = self.logger.critical
self.exception = self.logger.exception