python logging colorlog
1 import logging 2 LOG_LEVEL = logging.NOTSET 3 LOGFORMAT = "[%(log_color)s%(levelname)s] [%(log_color)s%(asctime)s] %(log_color)s%(filename)s [line:%(log_color)s%(lineno)d] : %(log_color)s%(message)s%(reset)s" 4 import colorlog 5 logging.root.setLevel(LOG_LEVEL) 6 ############ 7 #此配置是将日志输出到myapp.log 8 colorlog.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', filename='myapp.log', 9 filemode='w', datefmt='%a, %d %b %Y %H:%M:%S', ) 10 ############## 11 formatter = colorlog.ColoredFormatter(LOGFORMAT) 12 stream = logging.StreamHandler() 13 stream.setLevel(LOG_LEVEL) 14 stream.setFormatter(formatter) 15 log = logging.getLogger() 16 log.setLevel(LOG_LEVEL) 17 log.addHandler(stream) 18 log.debug("A quirky message only developers care about") 19 log.info("Curious users might want to know this") 20 log.warn("Something is wrong and any user should be informed") 21 log.error("Serious stuff, this is red for a reason") 22 log.critical("OH NO everything is on fire") 23 #####################################
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步