import logging logger = logging.getLogger() logger.setLevel(level = logging.INFO) handler = logging.FileHandler("log.txt") handler.setLevel(logging.INFO) formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘) handler.setFormatter(formatter) logger.addHandler(handler) logger.info("Start print log") logger.debug("Do something") logger.warning("Something maybe fail.") logger.info("Finish")
利用FileHandler将log写入文件,比basicConfig的好处是想写到哪个文件就写到哪个,basicConfig是一旦设置就不能更改
原文地址:https://www.cnblogs.com/mghhzAnne/p/12307751.html
时间: 2024-10-02 00:55:44