首页 > 编程语言 > 详细

Python colorlog 控制台带颜色输出

时间:2021-06-09 21:51:18      阅读:37      评论:0      收藏:0      [点我收藏+]

0、介绍

Python想要实现控制台的输出带颜色,可以使用logging+colorlog模块

1、字体颜色

import logging

import colorlog


def init_log():
    logger = logging.getLogger(ROOT)
    logger.setLevel(logging.DEBUG)
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    fmt_string = %(log_color)s[%(name)s][%(levelname)s]%(message)s
    # black red green yellow blue purple cyan 和 white
    log_colors = {
        DEBUG: white,
        INFO: green,
        WARNING: yellow,
        ERROR: red,
        CRITICAL: purple
        }
    fmt = colorlog.ColoredFormatter(fmt_string, log_colors=log_colors)
    stream_handler.setFormatter(fmt)
    logger.addHandler(stream_handler)
    return logger


if __name__ == __main__:
    log = init_log()
    log.debug(debug message)
    log.info(info message)
    log.warning(warning message)
    log.error(error message)
    log.critical(critical message)

技术分享图片

 2、背景色

import logging

import colorlog


def init_log():
    logger = logging.getLogger(ROOT)
    logger.setLevel(logging.DEBUG)
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    fmt_string = %(log_color)s[%(name)s][%(levelname)s]%(message)s
    # black red green yellow blue purple cyan 和 white
    log_colors = {
        DEBUG: black,bg_white,
        INFO: bg_green,
        WARNING: bg_yellow,
        ERROR: bg_red,
        CRITICAL: bg_purple
        }
    fmt = colorlog.ColoredFormatter(fmt_string, log_colors=log_colors)
    stream_handler.setFormatter(fmt)
    logger.addHandler(stream_handler)
    return logger


if __name__ == __main__:
    log = init_log()
    log.debug(debug message)
    log.info(info message)
    log.warning(warning message)
    log.error(error message)
    log.critical(critical message)

技术分享图片

 3、加粗

import logging

import colorlog


def init_log():
    logger = logging.getLogger(ROOT)
    logger.setLevel(logging.DEBUG)
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    fmt_string = %(log_color)s[%(name)s][%(levelname)s]%(message)s
    # black red green yellow blue purple cyan 和 white
    log_colors = {
        DEBUG: white,bold,
        INFO: green,bold,
        WARNING: yellow,bold,
        ERROR: red,bold,
        CRITICAL: purple,bold
        }
    fmt = colorlog.ColoredFormatter(fmt_string, log_colors=log_colors)
    stream_handler.setFormatter(fmt)
    logger.addHandler(stream_handler)
    return logger


if __name__ == __main__:
    log = init_log()
    log.debug(debug message)
    log.info(info message)
    log.warning(warning message)
    log.error(error message)
    log.critical(critical message)

技术分享图片

 参考 https://bearfly1990.github.io/2018/04/25/colorlog/

Python colorlog 控制台带颜色输出

原文:https://www.cnblogs.com/rainbow-tan/p/14868148.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!