123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import logging
- import colorlog
- import time
- from common.yaml_util import get_project_path, read_config_yaml
- class LoggerUtil:
- def create_log(self, logger_name='log',level='INFO'):
- level_relations = {
- 'debug': logging.DEBUG,
- 'info': logging.INFO,
- 'warning': logging.WARNING,
- 'error': logging.ERROR,
- 'crit': logging.CRITICAL
- }
- # 创建一个日志对象
- self.logger = logging.getLogger(logger_name)
- # 设置全局的日志级别(从低到高:debug调试<info信息<warning警告<error错误<critical严重)
- self.logger.setLevel(logging.DEBUG)
- if not self.logger.handlers:
- # ------文件日志------
- # 1创建文件日志路径
- self.file_log_path = get_project_path() + "/logs/" + read_config_yaml("log", "log_name") + str(
- int(time.time())) + ".log"
- # 2创建文件日志控制器
- self.file_hander = logging.FileHandler(self.file_log_path, encoding='UTF-8')
- # 3设置文件日志级别
- # file_log_level = str(read_config_yaml("log", "log_level")).lower()
- # 设置日志显示级别
- self.logger.setLevel(level_relations.get(level))
- # if file_log_level == "debug":
- # self.file_hander.setLevel(logging.DEBUG)
- # elif file_log_level == "info":
- # self.file_hander.setLevel(logging.INFO)
- # elif file_log_level == "warning":
- # self.file_hander.setLevel(logging.WARNING)
- # elif file_log_level == "error":
- # self.file_hander.setLevel(logging.ERROR)
- # elif file_log_level == "critical":
- # self.file_hander.setLevel(logging.CRITICAL)
- # else:
- # self.file_hander.setLevel(logging.DEBUG)
- # 4创建文件的格式
- self.file_hander.setFormatter(logging.Formatter(read_config_yaml("log", "log_format")))
- # 将文件日志的控制器加入日志对象
- self.logger.addHandler(self.file_hander)
- # 创建控制台日志
- # 1创建控制台日志控制器
- self.console_hander = logging.StreamHandler()
- # 2设置控制台日志级别
- self.logger.setLevel(level_relations.get(level))
- # console_log_level = str(read_config_yaml("log", "log_level")).lower()
- # if console_log_level == "debug":
- # self.console_hander.setLevel(logging.DEBUG)
- # elif console_log_level == "info":
- # self.console_hander.setLevel(logging.INFO)
- # elif console_log_level == "warning":
- # self.console_hander.setLevel(logging.WARNING)
- # elif console_log_level == "error":
- # self.console_hander.setLevel(logging.ERROR)
- # elif console_log_level == "critical":
- # self.console_hander.setLevel(logging.CRITICAL)
- # else:
- # self.console_hander.setLevel(logging.DEBUG)
- # formatter = colorlog.ColoredFormatter(
- # '%(log_color)s[%(asctime)s] [%(name)s] [%(levelname)s]: %(message)s',
- # log_colors=log_colors_config)
- formatter = colorlog.ColoredFormatter(
- '%(log_color)s[%(asctime)s] [%(name)s] [%(levelname)s]: %(message)s',
- log_colors=read_config_yaml("log","log_colors_config"))
- # 3创建控制台的日志格式
- self.console_hander.setFormatter(formatter)
- # 将控制台日志加入日志对象
- self.logger.addHandler(self.console_hander)
- # 返回包含文件日志控制器和控制台日志控制器的日志对象
- return self.logger
- # 错误日志的输出
- def error_log(message):
- LoggerUtil().create_log(level="error").error(message)
- # raise Exception(message)
- def warring_log(message):
- LoggerUtil().create_log(level="warning").warning(message)
- # raise Exception(message)
- # 信息日志的输出
- def logs(message):
- LoggerUtil().create_log(level="info").info(message)
- # raise Exception(message)
|