Python根据日志级别打印不同颜色的日志的方法示例
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Python根据⽇志级别打印不同颜⾊的⽇志的⽅法⽰例本⽂介绍了Python 根据⽇志级别打印不同颜⾊的⽇志的⽅法⽰例,分享给⼤家,具体如下:
# -*-coding:UTF-8-*-
import logging
import os
import time
class logger(object):
"""
终端打印不同颜⾊的⽇志,在pycharm中如果强⾏规定了⽇志的颜⾊,这个⽅法不会起作⽤,但是
对于终端,这个⽅法是可以打印不同颜⾊的⽇志的。
"""
#在这⾥定义StreamHandler,可以实现单例,所有的logger()共⽤⼀个StreamHandler
ch = logging.StreamHandler()
def __init__(self):
self.logger = logging.getLogger()
if not self.logger.handlers:
#如果self.logger没有handler,就执⾏以下代码添加handler
self.logger.setLevel(logging.DEBUG)
from serviceProgram.utils.FileUtil import FileUtil
rootPath = FileUtil.getProgrameRootPath()
self.log_path = rootPath + '/logs'
if not os.path.exists(self.log_path):
os.makedirs(self.log_path)
# 创建⼀个handler,⽤于写⼊⽇志⽂件
fh = logging.FileHandler(self.log_path + '/runlog' + time.strftime("%Y%m%d", time.localtime()) + '.log', encoding='utf-8')
fh.setLevel()
# 定义handler的输出格式
formatter = logging.Formatter('[%(asctime)s] - [%(levelname)s] - %(message)s')
fh.setFormatter(formatter)
# 给logger添加handler
self.logger.addHandler(fh)
def debug(self, message):
self.fontColor('\033[0;32m%s\033[0m')
self.logger.debug(message)
def info(self, message):
self.fontColor('\033[0;34m%s\033[0m')
(message)
def warning(self, message):
self.fontColor('\033[0;37m%s\033[0m')
self.logger.warning(message)
def error(self, message):
self.fontColor('\033[0;31m%s\033[0m')
self.logger.error(message)
def critical(self, message):
self.fontColor('\033[0;35m%s\033[0m')
self.logger.critical(message)
def fontColor(self, color):
#不同的⽇志输出不同的颜⾊
formatter = logging.Formatter(color % '[%(asctime)s] - [%(levelname)s] - %(message)s')
self.ch.setFormatter(formatter)
self.logger.addHandler(self.ch)
if __name__ == "__main__":
logger = logger()
("12345")
logger.debug("12345")
logger.warning("12345")
logger.error("12345")
实现过程:
终端的字符颜⾊是⽤转义序列控制的,是⽂本模式下的系统显⽰功能,和具体的语⾔⽆关。
转义序列是以ESC开头,即⽤\033来完成(ESC的ASCII码⽤⼗进制表⽰是27,⽤⼋进制表⽰就是033)。
书写格式:
开头部分:\033[显⽰⽅式;前景⾊;背景⾊m + 结尾部分:\033[0m
注意:开头部分的三个参数:显⽰⽅式,前景⾊,背景⾊是可选参数,可以只写其中的某⼀个;另外由于表⽰三个参数不同含义的数值都是唯⼀的没有重复的,所以三个参数的书写先后顺序没有固定要求,系统都能识别;但是,建议按照默认的格式规范书写。
对于结尾部分,其实也可以省略,但是为了书写规范,建议\033[***开头,\033[0m结尾。
数值表⽰的参数含义:
常见开头格式:
\033[0m 默认字体正常显⽰,不⾼亮
\033[32;0m 红⾊字体正常显⽰
\033[1;32;40m 显⽰⽅式: ⾼亮字体前景⾊:绿⾊背景⾊:⿊⾊
\033[0;31;46m 显⽰⽅式: 正常字体前景⾊:红⾊背景⾊:青⾊
实例:
(1)print("\033[1;31;40m您输⼊的帐号或密码错误!\033[0m")
上⽅代码的输出格式为:字体⾼亮,红⾊前景,黄⾊背景 PS:前景⾊也就是字体的颜⾊
(2)print("\033[0;31m%s\033[0m" % "输出红⾊字符")
#上⽅代码的输出格式为:字体默认,红⾊前景
LOG_INFO='INFO'
LOG_ERROR='ERROR'
LOG_WARNING='WARNING'
LOG_NOTIFY='NOTIFY'
LOG_DEBUG='DEBUG'
LOG_USER='USER'
def info_log(value):
if log_level > 3:
print("\033[0;37;40m%s\033[0m"%value)
def error_log(value):
if log_level != 0:
print("\033[0;31;40m%s\033[0m"%value)
def warning_log(value):
if log_level > 1:
print("\033[0;33;40m%s\033[0m"%value)
def debug_log(value):
if log_level > 5:
print("\033[0;34;40m%s\033[0m"%value)
def notify_log(value):
if log_level > 2:
print("\033[0;36;40m%s\033[0m"%value)
def user_log(value):
if log_level > 4:
print("\033[0;32;40m%s\033[0m"%value)
def ZLOG(log_type,value):
switcher={
'INFO':info_log,
'ERROR':error_log,
'WARNING':warning_log,
'DEBUG':debug_log,
'NOTIFY':notify_log,
'USER':user_log
}
return switcher[log_type](value)
test="hello world"
ZLOG(LOG_INFO,"output info log %s"%test)
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。