logging_service.py 657 B

12345678910111213141516171819202122232425
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:fenc=utf-8
  4. import logging
  5. COLORS = {
  6. 'DEBUG': '\033[0m', # 蓝色
  7. 'INFO': '\033[32m', # 绿色
  8. 'WARNING': '\x1b[33;20m', # 黄色
  9. 'ERROR': '\033[91m', # 红色
  10. 'CRITICAL': '\033[95m', # 紫色
  11. 'RESET': '\033[0m', # 重置颜色
  12. }
  13. class ColoredFormatter(logging.Formatter):
  14. def format(self, record):
  15. # 获取原始日志消息
  16. message = super().format(record)
  17. # 根据日志等级添加颜色
  18. if record.levelname in COLORS:
  19. message = f"{COLORS[record.levelname]}{message}{COLORS['RESET']}"
  20. return message