网上找了一堆的想要的带有输出颜色的日志输出,感觉写的都挺好,但是代码量很多,对于小白来说属实很难,所以我还是自己研究加网上参考吧,下面分享一下(如果写的与他人有冲突或一样,可能纯属巧合,我发布的晚可以联系我,我会删掉,反之请您删掉,谢谢!如果帮到了您,请给予支持)。废话不多说,直接上代码效果:
具体代码如下:
#------------------------logging日志输出颜色封装-----------------------
import logging
class Log:
logging.basicConfig(
format=‘[%(asctime)s] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s’,
level=logging.DEBUG,
)
@classmethod
def debug(cls,msg):
return logging.debug(("\033[1;35m {} \033[0m").format(msg))
@classmethod
def info(cls, msg):
return logging.info(("\033[1;36m {} \033[0m").format(msg))
@classmethod
def warning(cls, msg):
return logging.warning(("\033[1;33m {} \033[0m").format(msg))
@classmethod
def error(cls, msg):
return logging.error(("\033[1;34m {} \033[0m").format(msg))
@classmethod
def critical(cls, msg):
return logging.critical(("\033[1;31m {} \033[0m").format(msg))
if name == ‘main’:
Log.debug(‘this is debug’)
Log.info(‘this is info’)
Log.warning(‘this is warning’)
Log.error(‘this is error’)
Log.critical(‘this is critical’)
#--------------------------------logging颜色封装完毕-----------------------------
代码写的比较low,高手请谅解,不喜勿喷