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

作者:无霸独尊 时间:2022-08-18 01:47:25 

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日志通过不同的等级打印不同的颜色(示例代码)

来源:https://blog.csdn.net/weixin_44174099/article/details/112535345

标签:python,日志级别,颜色
0
投稿

猜你喜欢

  • Python打印“菱形”星号代码方法

    2023-02-05 17:34:42
  • Perl中的正则表达式介绍

    2023-08-11 21:14:25
  • Springboot使用influxDB时序数据库的实现

    2024-01-18 13:42:10
  • Opencv对象追踪的示例代码

    2021-03-02 06:35:38
  • 浅析Python中的for 循环

    2023-12-16 10:05:20
  • 几个缩减MySQL以节省磁盘空间的建议

    2024-01-17 11:41:23
  • Python内置函数的用法实例教程

    2021-03-02 01:08:15
  • pandas DataFrame 根据多列的值做判断,生成新的列值实例

    2023-11-11 01:48:11
  • Python中你所不知道的星号 * 用法

    2022-08-06 15:25:43
  • python实现3D地图可视化

    2023-08-27 10:24:50
  • python中json格式数据输出的简单实现方法

    2021-03-04 22:19:19
  • Python3使用requests登录人人影视网站的方法

    2021-10-04 15:42:19
  • Python 查看list中是否含有某元素的方法

    2023-07-29 21:46:08
  • 详解Python迭代和迭代器

    2023-04-30 23:31:13
  • centos系统升级python 2.7.3

    2023-08-18 06:40:52
  • PHP PDOStatement::bindValue讲解

    2023-06-14 11:16:02
  • 用 Python 定义 Schema 并生成 Parquet 文件详情

    2021-09-24 14:14:18
  • SQL Server 监控磁盘IO错误,msdb.dbo.suspect_pages

    2024-01-22 09:26:44
  • Python爬虫程序架构和运行流程原理解析

    2023-10-04 16:04:24
  • mysql数据库单表最大存储依据详解

    2024-01-16 03:02:24
  • asp之家 网络编程 m.aspxhome.com