apppublic/appPublic/app_logger.py

43 lines
1.1 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-04-06 14:05:35 +08:00
level = levels.get('info')
2022-03-23 12:24:04 +08:00
def create_logger(name, formater=defaultfmt, levelname='info', file=None):
2022-03-25 20:44:26 +08:00
global logfile, level
if logfile == -1:
logfile = file
2022-03-23 12:24:04 +08:00
ret = logging.getLogger(name)
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