当前位置:网站首页>Colorlog combined with logging to print colored logs

Colorlog combined with logging to print colored logs

2022-07-06 08:24:00 Don't seek great wealth, just seek wealth to rival the country

import logging
import colorlog

  # log_colors={
    
  # 'DEBUG':'cyan',
  # 'INFO': 'green',
  # 'WARNING':'yellow',
  # 'ERROR': 'red',
  # 'CRITICAL':'red,bg_white'
                            },
 # First step : Create a log collector logger
lg=logging.getLogger(__file__)
# The second step : Modify the output level of the log 
lg.setLevel(logging.DEBUG)

# Set the output channel -- Output to console 
# sh=colorlog.StreamHandler()
sh1=logging.StreamHandler()

# The third step : Set the output log content format 
fh=logging.FileHandler(r"C:\Users\jam96\PycharmProjects\all_module\logging_test\data\jxd.log",encoding="utf-8")
lg.addHandler(sh1)
lg.addHandler(fh)
#%(log_color)s
cl=colorlog.ColoredFormatter(fmt="**%(log_color)s** %(asctime)s %(pathname)s %(funcName)s [%(lineno)d] %(levelname)s %(message)s")
fm=logging.Formatter("%(asctime)s %(pathname)s %(funcName)s [%(lineno)d] %(levelname)s %(message)s")

sh1.setFormatter(cl)
fh.setFormatter(fm)

if __name__ == '__main__':
 for i  in range(10):
    lg.debug("12345")
    lg.info("abcde")
    lg.warning("******")
    lg.error("good boy")
    lg.critical("bad girl")
原网站

版权声明
本文为[Don't seek great wealth, just seek wealth to rival the country]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060819357509.html