apppublic/appPublic/app_logger.py

48 lines
1.2 KiB
Python
Raw Normal View History

2022-03-23 12:24:04 +08:00
import sys
import logging
from functools import partial
levels={
"debug":logging.DEBUG,
"info":logging.INFO,
"warning":logging.WARNING,
"error":logging.error,
"critical":logging.CRITICAL
}
2022-04-06 14:05:35 +08:00
defaultfmt = '%(asctime)s[%(name)s][%(levelname)s][%(filename)s:%(lineno)s]%(message)s'
2022-03-25 20:44:26 +08:00
logfile = -1
2022-06-23 16:37:46 +08:00
g_levelname='info'
2022-04-06 14:05:35 +08:00
level = levels.get('info')
2022-03-23 12:24:04 +08:00
2023-06-08 11:22:58 +08:00
def create_logger(name, formater=defaultfmt, levelname=None, file=None):
2023-06-08 11:38:48 +08:00
global logfile, level, g_levelname
2022-03-25 20:44:26 +08:00
if logfile == -1:
logfile = file
2022-03-23 12:24:04 +08:00
ret = logging.getLogger(name)
2023-06-08 11:22:58 +08:00
if levelname:
g_levelname = levelname
else:
levelname = g_levelname
2022-04-06 14:05:35 +08:00
level = levels.get(levelname, levels.get('info'))
2022-03-23 12:24:04 +08:00
ret.setLevel(level)
format = logging.Formatter(formater)
file_handler = None
2022-03-25 20:44:26 +08:00
if logfile is not None:
file_handler = logging.FileHandler(logfile)
2022-03-23 12:24:04 +08:00
else:
file_handler = logging.StreamHandler()
file_handler.setFormatter(format)
ret.addHandler(file_handler)
return ret
2022-03-25 20:44:26 +08:00
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