脚本专栏 
首页 > 脚本专栏 > 浏览文章

python日志通过不同的等级打印不同的颜色(示例代码)

(编辑:jimmy 日期: 2024/11/2 浏览:3 次 )

1,不用第三方库

# coding: utf-8
import logging

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[1;%dm"

COLORS = {
 'WARNING': GREEN,
 'INFO': WHITE,
 'DEBUG': BLUE,
 'CRITICAL': YELLOW,
 'ERROR': RED
}

class ColoredFormatter(logging.Formatter):
 def __init__(self, msg, use_color=True):
  logging.Formatter.__init__(self, msg)
  self.use_color = use_color

 def format(self, record):
  levelname = record.levelname
  message = str(record.msg)
  funcName = record.funcName
  if self.use_color and levelname in COLORS:
   levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
   message_color = COLOR_SEQ % (30 + COLORS[levelname]) + message + RESET_SEQ
   funcName_color = COLOR_SEQ % (30 + COLORS[levelname]) + funcName + RESET_SEQ
   record.levelname = levelname_color
   record.msg = message_color
   record.funcName = funcName_color
  return logging.Formatter.format(self, record)

LOGFORMAT = "[%(asctime)s][%(name)s] [%(levelname)s] (%(filename)s:%(funcName)s:%(lineno)d) %(message)s"
LOG_LEVEL = logging.DEBUG
formatter = ColoredFormatter(LOGFORMAT)
stream = logging.StreamHandler()
stream.setLevel(LOG_LEVEL)
stream.setFormatter(formatter)
logging.root.setLevel(LOG_LEVEL)
log = logging.getLogger('logconfig')
log.setLevel(LOG_LEVEL)
log.addHandler(stream)
def Logging(name):
 log = logging.getLogger(name)
 log.setLevel(LOG_LEVEL)
 log.addHandler(stream)
 return log

if __name__ == '__main__':
 logger = Logging(__name__)
 logger.info(123123)
 logger.debug(123123)
 logger.error(123123)
 logger.warning(123123)

 

python日志通过不同的等级打印不同的颜色(示例代码)

2,使用colorlog pip install colorlog

# coding: utf-8
# coding: utf-8
from colorlog import ColoredFormatter
import logging

LOG_LEVEL = logging.DEBUG

LOGFORMAT = "[%(asctime)s][%(name)s] [%(log_color)s**%(levelname)s**%(reset)s] [%(filename)s:%(funcName)s:%(log_color)s%(lineno)d%(reset)s] %(log_color)s%(message)s%(reset)s"
logging.root.setLevel(LOG_LEVEL)
formatter = ColoredFormatter(LOGFORMAT)

stream = logging.StreamHandler()
stream.setLevel(LOG_LEVEL)
stream.setFormatter(formatter)

log = logging.getLogger('logconfig')
log.setLevel(LOG_LEVEL)
log.addHandler(stream)

def Logging(name):
 log = logging.getLogger(name)
 log.setLevel(LOG_LEVEL)
 log.addHandler(stream)
 return log

if __name__ == '__main__':
 logging = Logging("test")
 logging.info(123123)
 logging.warning(123123)
 logging.debug(123123)

python日志通过不同的等级打印不同的颜色(示例代码)

3,华丽的日志

pip install logbook termcc dataclasses

#coding: utf-8
from logbook import Logger
from termcc.helper.logger import sample_flask as setup_logger
setup_logger()

logging = Logger(__name__)
logging.info("123123")
logging.debug("123123")
logging.warn("123123")

python日志通过不同的等级打印不同的颜色(示例代码)

上一篇:python中编写函数并调用的知识点总结
下一篇:Python jieba库分词模式实例用法
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap