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