代码原理很简单,在需要加颜色的地方加上控制颜色显示的语义即可。
第三方模块
coloredlogs
代码语言:javascript复制# Create a logger object.
import logging
logger = logging.getLogger('your-module')
# Initialize coloredlogs.
import coloredlogs
coloredlogs.install(level='DEBUG')
# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warn("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")
效果图
纯代码实现
代码语言:javascript复制import logging
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
#The background is set with 40 plus the number of the color, and the foreground with 30
#These are the sequences need to get colored ouput
RESET_SEQ = "