|
@@ -1,11 +1,21 @@
|
|
|
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'):
|
|
|
+ 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)
|
|
@@ -19,19 +29,22 @@ class LoggerUtil:
|
|
|
# 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()
|
|
|
- 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)
|
|
|
+ # 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")))
|
|
|
# 将文件日志的控制器加入日志对象
|
|
@@ -40,21 +53,31 @@ class LoggerUtil:
|
|
|
# 1创建控制台日志控制器
|
|
|
self.console_hander = logging.StreamHandler()
|
|
|
# 2设置控制台日志级别
|
|
|
- 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)
|
|
|
+ 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(logging.Formatter(read_config_yaml("log", "log_format")))
|
|
|
+ self.console_hander.setFormatter(formatter)
|
|
|
# 将控制台日志加入日志对象
|
|
|
self.logger.addHandler(self.console_hander)
|
|
|
|
|
@@ -64,14 +87,18 @@ class LoggerUtil:
|
|
|
|
|
|
# 错误日志的输出
|
|
|
def error_log(message):
|
|
|
- LoggerUtil().create_log().error(message)
|
|
|
- raise Exception(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().info(message)
|
|
|
-
|
|
|
+ LoggerUtil().create_log(level="info").info(message)
|
|
|
+ # raise Exception(message)
|
|
|
|
|
|
-if __name__ == '__main__':
|
|
|
- error_log("车务通")
|