log_util.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import logging
  2. import colorlog
  3. import time
  4. from common.yaml_util import get_project_path, read_config_yaml
  5. class LoggerUtil:
  6. def create_log(self, logger_name='log',level='INFO'):
  7. level_relations = {
  8. 'debug': logging.DEBUG,
  9. 'info': logging.INFO,
  10. 'warning': logging.WARNING,
  11. 'error': logging.ERROR,
  12. 'crit': logging.CRITICAL
  13. }
  14. # 创建一个日志对象
  15. self.logger = logging.getLogger(logger_name)
  16. # 设置全局的日志级别(从低到高:debug调试<info信息<warning警告<error错误<critical严重)
  17. self.logger.setLevel(logging.DEBUG)
  18. if not self.logger.handlers:
  19. # ------文件日志------
  20. # 1创建文件日志路径
  21. self.file_log_path = get_project_path() + "/logs/" + read_config_yaml("log", "log_name") + str(
  22. int(time.time())) + ".log"
  23. # 2创建文件日志控制器
  24. self.file_hander = logging.FileHandler(self.file_log_path, encoding='UTF-8')
  25. # 3设置文件日志级别
  26. # file_log_level = str(read_config_yaml("log", "log_level")).lower()
  27. # 设置日志显示级别
  28. self.logger.setLevel(level_relations.get(level))
  29. # if file_log_level == "debug":
  30. # self.file_hander.setLevel(logging.DEBUG)
  31. # elif file_log_level == "info":
  32. # self.file_hander.setLevel(logging.INFO)
  33. # elif file_log_level == "warning":
  34. # self.file_hander.setLevel(logging.WARNING)
  35. # elif file_log_level == "error":
  36. # self.file_hander.setLevel(logging.ERROR)
  37. # elif file_log_level == "critical":
  38. # self.file_hander.setLevel(logging.CRITICAL)
  39. # else:
  40. # self.file_hander.setLevel(logging.DEBUG)
  41. # 4创建文件的格式
  42. self.file_hander.setFormatter(logging.Formatter(read_config_yaml("log", "log_format")))
  43. # 将文件日志的控制器加入日志对象
  44. self.logger.addHandler(self.file_hander)
  45. # 创建控制台日志
  46. # 1创建控制台日志控制器
  47. self.console_hander = logging.StreamHandler()
  48. # 2设置控制台日志级别
  49. self.logger.setLevel(level_relations.get(level))
  50. # console_log_level = str(read_config_yaml("log", "log_level")).lower()
  51. # if console_log_level == "debug":
  52. # self.console_hander.setLevel(logging.DEBUG)
  53. # elif console_log_level == "info":
  54. # self.console_hander.setLevel(logging.INFO)
  55. # elif console_log_level == "warning":
  56. # self.console_hander.setLevel(logging.WARNING)
  57. # elif console_log_level == "error":
  58. # self.console_hander.setLevel(logging.ERROR)
  59. # elif console_log_level == "critical":
  60. # self.console_hander.setLevel(logging.CRITICAL)
  61. # else:
  62. # self.console_hander.setLevel(logging.DEBUG)
  63. # formatter = colorlog.ColoredFormatter(
  64. # '%(log_color)s[%(asctime)s] [%(name)s] [%(levelname)s]: %(message)s',
  65. # log_colors=log_colors_config)
  66. formatter = colorlog.ColoredFormatter(
  67. '%(log_color)s[%(asctime)s] [%(name)s] [%(levelname)s]: %(message)s',
  68. log_colors=read_config_yaml("log","log_colors_config"))
  69. # 3创建控制台的日志格式
  70. self.console_hander.setFormatter(formatter)
  71. # 将控制台日志加入日志对象
  72. self.logger.addHandler(self.console_hander)
  73. # 返回包含文件日志控制器和控制台日志控制器的日志对象
  74. return self.logger
  75. # 错误日志的输出
  76. def error_log(message):
  77. LoggerUtil().create_log(level="error").error(message)
  78. # raise Exception(message)
  79. def warring_log(message):
  80. LoggerUtil().create_log(level="warning").warning(message)
  81. # raise Exception(message)
  82. # 信息日志的输出
  83. def logs(message):
  84. LoggerUtil().create_log(level="info").info(message)
  85. # raise Exception(message)