<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python 打印彩色日志

          共 3929字,需瀏覽 8分鐘

           ·

          2022-12-31 08:07

          我相信每一個(gè)開發(fā)者都有打印日志的習(xí)慣,好看的日志可以加快調(diào)試的速度,可以更好的了解程序中發(fā)生的事情。本文分享一個(gè)技巧,可以讓 Python 在控制臺(tái)輸出彩色的日志。

          安裝 coloredlogs

          pip install coloredlogs

          使用

          首先,和正常打印日志一樣,我們創(chuàng)建一個(gè) logger

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

          然后,把 logger 安裝到 coloredlogs:

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

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

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

          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'),
              )
          )

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

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

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

          logger.debug(msg="this is a debug message")
          logger.info(msg="this is an info message")
          logger.warning(msg="this is a warning message")
          logger.error(msg="this is an error message")
          logger.critical(msg="this is a critical message")

          效果圖如下:

          完整代碼如下:

          import logging
          import coloredlogs
          import sys


          ## 配置 logger
          logging.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'),
              )
          )

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

          ## output
          logger.debug(msg="this is a debug message")
          logger.info(msg="this is an info message")
          logger.warning(msg="this is a warning message")
          logger.error(msg="this is an error message")
          logger.critical(msg="this is a critical message")

          最后的話

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


          瀏覽 89
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本三级片网站久射 | 干逼逼| 91av在线视频会员 | 日逼a| 天天干天天日麻豆 |