python日志通過不同的等級(jí)打印不同的顏色(示例代碼)
1,不用第三方庫(kù)
# 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)
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)
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")
到此這篇關(guān)于python日志通過不同的等級(jí)打印不同的顏色的文章就介紹到這了,更多相關(guān)python根據(jù)日志級(jí)別打印顏色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Django 實(shí)現(xiàn)簡(jiǎn)單注冊(cè)功能過程詳解
這篇文章主要介紹了Python Django 實(shí)現(xiàn)簡(jiǎn)單注冊(cè)功能過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Python開發(fā)之os與os.path的使用小結(jié)
這篇文章主要介紹了Python開發(fā)之os與os.path的使用小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-05-05Python drop方法刪除列之inplace參數(shù)實(shí)例
這篇文章主要介紹了Python drop方法刪除列之inplace參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python獲取微信企業(yè)號(hào)打卡數(shù)據(jù)并生成windows計(jì)劃任務(wù)
由于公司的系統(tǒng)用的是Java版本,開通了企業(yè)號(hào)打卡之后又沒有預(yù)算讓供應(yīng)商做數(shù)據(jù)對(duì)接,所以只能自己搗鼓這個(gè),以下是個(gè)人設(shè)置的一些內(nèi)容,僅供大家參考2019-04-04Python爬蟲實(shí)戰(zhàn)之用selenium爬取某旅游網(wǎng)站
上一篇我們已經(jīng)知道怎么簡(jiǎn)單使用selenium了,那么我們就從這篇博客來動(dòng)手爬取網(wǎng)站吧,文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下2021-06-06Python實(shí)現(xiàn)將json文件生成C語言的結(jié)構(gòu)體的腳本分享
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)將json文件生成C語言的結(jié)構(gòu)體,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09