Python:colorlog的三个例子
生活随笔
收集整理的這篇文章主要介紹了
Python:colorlog的三个例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
例1:默認的log_colors
import logging from logging.handlers import RotatingFileHandler from colorlog import ColoredFormatter#第一步:創建一個日志收集器logger logger = logging.getLogger("autotest")#第二步:修改日志的輸出級別 logger.setLevel(logging.DEBUG)#第三步:設置輸出的日志內容格式 fmt = "%(log_color)s%(asctime)s %(log_color)s%(filename)s %(log_color)s%(funcName)s [line:%(log_color)s%(lineno)d] %(log_color)s%(levelname)s %(log_color)s%(message)s" datefmt = '%a, %d %b %Y %H:%M:%S'formatter = ColoredFormatter(fmt=fmt,datefmt=datefmt,reset=True,secondary_log_colors={},style='%')#設置輸出渠道--輸出到控制臺 hd_1 = logging.StreamHandler() #在handler上指定日志內容格式 hd_1.setFormatter(formatter)#第五步:將headler添加到日志logger上 logger.addHandler(hd_1)#第六步:調用輸出方法 logger.debug("我是debug級別的日志") logger.info("我是info級別的日志") logger.warning("我是warning級別的日志") logger.critical("我的critical級別的日志") logger.error("我是error級別的日志輸出")默認的color_colors的值為:
代碼運行結果:
例2:自定義log_colors
代碼運行結果:
例3:secondary_log_color的使用
代碼運行結果:
總結
以上是生活随笔為你收集整理的Python:colorlog的三个例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的reduce函数
- 下一篇: python中的set类型