• <big id="2pauu"><ruby id="2pauu"></ruby></big>
    <td id="2pauu"><ruby id="2pauu"></ruby></td>
    <p id="2pauu"><strong id="2pauu"><xmp id="2pauu"></xmp></strong></p>
    <td id="2pauu"></td>
    1. <tr id="2pauu"><strong id="2pauu"></strong></tr>
    2. 您的位置:首頁 >聚焦 >

      環球速遞!Python 打印彩色日志

      2022-12-31 08:36:32    來源:程序員客棧

      我相信每一個開發者都有打印日志的習慣,好看的日志可以加快調試的速度,可以更好的了解程序中發生的事情。本文分享一個技巧,可以讓 Python 在控制臺輸出彩色的日志。

      安裝 coloredlogs

      pipinstallcoloredlogs

      使用

      首先,和正常打印日志一樣,我們創建一個 logger


      (資料圖片僅供參考)

      logging.basicConfig()logger=logging.getLogger(name="mylogger")

      然后,把 logger 安裝到 coloredlogs:

      coloredlogs.install(logger=logger)logger.propagate=False

      logger.propagate = False確保 coloredlogs 不會將我們的日志事件傳遞給根 logger,這可以防止我們重復記錄每個事件。

      我們想為我們的控制臺輸出添加一些樣式,可以在這里定義:

      coloredFormatter=coloredlogs.ColoredFormatter(fmt="[%(name)s]%(asctime)s%(funcName)s%(lineno)-3d%(message)s",level_styles=dict(debug=dict(color="white"),info=dict(color="blue"),warning=dict(color="yellow",bright=True),error=dict(color="red",bold=True,bright=True),critical=dict(color="black",bold=True,background="red"),),field_styles=dict(name=dict(color="white"),asctime=dict(color="white"),funcName=dict(color="white"),lineno=dict(color="white"),))

      接下來就和正常使用日志一樣了,配置一個流處理器,讓日志顯示在控制臺:

      ch=logging.StreamHandler(stream=sys.stdout)ch.setFormatter(fmt=coloredFormatter)logger.addHandler(hdlr=ch)logger.setLevel(level=logging.DEBUG)

      接下來就可以輸入日志信息了:

      logger.debug(msg="thisisadebugmessage")logger.info(msg="thisisaninfomessage")logger.warning(msg="thisisawarningmessage")logger.error(msg="thisisanerrormessage")logger.critical(msg="thisisacriticalmessage")

      效果圖如下:

      完整代碼如下:

      importloggingimportcoloredlogsimportsys##配置loggerlogging.basicConfig()logger=logging.getLogger(name="mylogger")coloredlogs.install(logger=logger)logger.propagate=False##配置顏色coloredFormatter=coloredlogs.ColoredFormatter(fmt="[%(name)s]%(asctime)s%(funcName)s%(lineno)-3d%(message)s",level_styles=dict(debug=dict(color="white"),info=dict(color="blue"),warning=dict(color="yellow",bright=True),error=dict(color="red",bold=True,bright=True),critical=dict(color="black",bold=True,background="red"),),field_styles=dict(name=dict(color="white"),asctime=dict(color="white"),funcName=dict(color="white"),lineno=dict(color="white"),))##配置StreamHandlerch=logging.StreamHandler(stream=sys.stdout)ch.setFormatter(fmt=coloredFormatter)logger.addHandler(hdlr=ch)logger.setLevel(level=logging.DEBUG)##outputlogger.debug(msg="thisisadebugmessage")logger.info(msg="thisisaninfomessage")logger.warning(msg="thisisawarningmessage")logger.error(msg="thisisanerrormessage")logger.critical(msg="thisisacriticalmessage")

      最后的話

      本文分享了一種輸出彩色日志的方法,感覺不錯的話,請分享給身邊的程序員們,祝編碼愉快。

      關鍵詞: 正常使用

      相關閱讀

      欧美性交网站
    3. <big id="2pauu"><ruby id="2pauu"></ruby></big>
      <td id="2pauu"><ruby id="2pauu"></ruby></td>
      <p id="2pauu"><strong id="2pauu"><xmp id="2pauu"></xmp></strong></p>
      <td id="2pauu"></td>
      1. <tr id="2pauu"><strong id="2pauu"></strong></tr>