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日志级别打印颜色

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?