Python 彩色命令行输出

效果:

下面描述如何来实现。

安装coloredlogs:

1
pip3 install --user coloredlogs

就可以了,使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import logging

import coloredlogs


FIELD_STYLES = dict(
asctime=dict(color='green'),
hostname=dict(color='magenta'),
levelname=dict(color='green', bold=coloredlogs.CAN_USE_BOLD_FONT),
filename=dict(color='magenta'),
name=dict(color='blue'),
threadName=dict(color='green')
)

LEVEL_STYLES = dict(
debug=dict(color='green'),
info=dict(color='cyan'),
warning=dict(color='yellow'),
error=dict(color='red'),
critical=dict(color='red', bold=coloredlogs.CAN_USE_BOLD_FONT)
)

logger = logging.getLogger('tos')
coloredlogs.install(
level="DEBUG",
fmt="[%(levelname)s] [%(asctime)s] [%(filename)s:%(lineno)d] %(message)s",
level_styles=LEVEL_STYLES,
field_styles=FIELD_STYLES)

logger.debug('This is Debug mode')
logger.info('This is info mode')
logger.warn('This is warn mode')
logger.error('This is error mode')
logger.critical('This is critical mode')