import logging
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s %(name)s %(levelname)s %(pathname)s %(message)s ", # 配置输出日志格式
datefmt='%Y-%m-%d %H:%M:%S %a' ,
filename=r".\test.log" # 有了filename参数就不会直接输出显示到控制台,而是直接写入文件
)
logging.debug("msg1")
logging.info("msg2")
logging.warning("msg3")
logging.error("msg4")
logging.critical("msg5")
输出到控制台的结果:
2020-04-23 17:57:38 Thu root DEBUG D:/py save/test2.py msg1
2020-04-23 17:57:38 Thu root INFO D:/py save/test2.py msg2
2020-04-23 17:57:38 Thu root WARNING D:/py save/test2.py msg3
2020-04-23 17:57:38 Thu root ERROR D:/py save/test2.py msg4
2020-04-23 17:57:38 Thu root CRITICAL D:/py save/test2.py msg5