From acd265a38cf05ac7661ba22288df0210d84a5ba2 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 27 May 2022 11:38:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=20`logger`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger.py | 62 +++++++++++++++++++++---------------------------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/logger.py b/logger.py index d5f36974..b75cfe33 100644 --- a/logger.py +++ b/logger.py @@ -4,68 +4,52 @@ import colorlog import os current_path = os.path.realpath(os.getcwd()) -log_path = os.path.join(current_path, 'logs') +log_path = os.path.join(current_path, "logs") if not os.path.exists(log_path): os.mkdir(log_path) -log_file_name = os.path.join(log_path, 'log.log') +log_file_name = os.path.join(log_path, "log.log") log_colors_config = { - 'DEBUG': 'cyan', - 'INFO': 'green', - 'WARNING': 'yellow', - 'ERROR': 'red', - 'CRITICAL': 'red', + "DEBUG": "cyan", + "INFO": "green", + "WARNING": "yellow", + "ERROR": "red", + "CRITICAL": "red", } class Logger: def __init__(self): self.logger = logging.getLogger("TGPaimonBot") + root_logger = logging.getLogger() + root_logger.setLevel(logging.CRITICAL) self.logger.setLevel(logging.INFO) self.formatter = colorlog.ColoredFormatter( - '%(log_color)s[%(asctime)s] [%(levelname)s] - %(message)s', log_colors=log_colors_config) - self.formatter2 = colorlog.ColoredFormatter( - '[%(asctime)s] [%(levelname)s] - %(message)s') + "%(log_color)s[%(asctime)s] [%(levelname)s] - %(message)s", log_colors=log_colors_config) + self.formatter2 = logging.Formatter("[%(asctime)s] [%(levelname)s] - %(message)s") + fh = RotatingFileHandler(filename=log_file_name, maxBytes=1024 * 1024 * 5, backupCount=5, + encoding="utf-8") + fh.setFormatter(self.formatter2) + root_logger.addHandler(fh) + + ch = colorlog.StreamHandler() + ch.setFormatter(self.formatter) + root_logger.addHandler(ch) def getLogger(self): return self.logger - def _console(self, level, message, exc_info=None): - fh = RotatingFileHandler(filename=log_file_name, maxBytes=1024 * 1024 * 5, backupCount=5, - encoding='utf-8') - fh.setLevel(logging.INFO) - fh.setFormatter(self.formatter2) - self.logger.addHandler(fh) - - ch = colorlog.StreamHandler() - ch.setLevel(logging.INFO) - ch.setFormatter(self.formatter) - self.logger.addHandler(ch) - - if level == 'info': - self.logger.info(msg=message, exc_info=exc_info) - elif level == 'debug': - self.logger.debug(msg=message, exc_info=exc_info) - elif level == 'warning': - self.logger.warning(msg=message, exc_info=exc_info) - elif level == 'error': - self.logger.error(msg=message, exc_info=exc_info) - - self.logger.removeHandler(ch) - self.logger.removeHandler(fh) - fh.close() - def debug(self, msg, exc_info=None): - self._console('debug', msg, exc_info) + self.logger.debug(msg=msg, exc_info=exc_info) def info(self, msg, exc_info=None): - self._console('info', msg, exc_info) + self.logger.info(msg=msg, exc_info=exc_info) def warning(self, msg, exc_info=None): - self._console('warning', msg, exc_info) + self.logger.warning(msg=msg, exc_info=exc_info) def error(self, msg, exc_info=None): - self._console('error', msg, exc_info) + self.logger.error(msg=msg, exc_info=exc_info) Log = Logger()